Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
textread

Subject: textread

From: Jessica

Date: 2 Jun, 2008 14:10:03

Message: 1 of 11

Hi,

I have a .txt file with two columns, with a mixture of
numbers and letters:

John Jenny
David 23
Peter 35
Mark
David Sue

I want to be able to replace the bottom two lines with this:

Steve 42
John 23
Adam
Ben

Such that the final output is:

John Jenny
David 23
Peter 35
Steve 42
John 23
Adam
Ben

and is saved as a .txt file.

Does anyone have any suggestions?

Thanks in advance!

Subject: textread

From: Ian Clarkson

Date: 2 Jun, 2008 15:09:02

Message: 2 of 11

"Jessica " <jyorzinski@ucdavis.edu> wrote in message
<g20uvr$kkk$1@fred.mathworks.com>...
> Hi,
>
> I have a .txt file with two columns, with a mixture of
> numbers and letters:
>
> John Jenny
> David 23
> Peter 35
> Mark
> David Sue
>
> I want to be able to replace the bottom two lines with this:
>
> Steve 42
> John 23
> Adam
> Ben
>
> Such that the final output is:
>
> John Jenny
> David 23
> Peter 35
> Steve 42
> John 23
> Adam
> Ben
>
> and is saved as a .txt file.
>
> Does anyone have any suggestions?
>
> Thanks in advance!
>

I think this is a case of having a job that Notepad could do
better than MATLAB. Unless you want to replace the last two
lines of a bunch of text files with the same four lines, or
something along those lines, there's really no reason to use
MATLAB for this. It would help if you described your overall
goal so that the algorithm does what you want it to in the end.

Subject: textread

From: Jessica

Date: 2 Jun, 2008 22:33:02

Message: 3 of 11

"Ian Clarkson" <ovoidkumquat@hotmail.com> wrote in message
<g212ee$gp6$1@fred.mathworks.com>...
> "Jessica " <jyorzinski@ucdavis.edu> wrote in message
> <g20uvr$kkk$1@fred.mathworks.com>...
> > Hi,
> >
> > I have a .txt file with two columns, with a mixture of
> > numbers and letters:
> >
> > John Jenny
> > David 23
> > Peter 35
> > Mark
> > David Sue
> >
> > I want to be able to replace the bottom two lines with
this:
> >
> > Steve 42
> > John 23
> > Adam
> > Ben
> >
> > Such that the final output is:
> >
> > John Jenny
> > David 23
> > Peter 35
> > Steve 42
> > John 23
> > Adam
> > Ben
> >
> > and is saved as a .txt file.
> >
> > Does anyone have any suggestions?
> >
> > Thanks in advance!
> >
>
> I think this is a case of having a job that Notepad could
do
> better than MATLAB. Unless you want to replace the last
two
> lines of a bunch of text files with the same four lines,
or
> something along those lines, there's really no reason to
use
> MATLAB for this. It would help if you described your
overall
> goal so that the algorithm does what you want it to in
the end.
>

Thanks for the response. You are correct in that I am
wanting to replace the last three lines of the text of many
different .txt files with the same four lines of new text.
I can copy and paste the four lines into each .txt file but
it would take a very long time to do so because I have
hundreds of .txt files.

Subject: textread

From: us

Date: 3 Jun, 2008 00:15:05

Message: 4 of 11

"Jessica ":
<SNIP appending boring data to a file...

> John Jenny
> David 23
> Peter 35
> Mark
> David Sue

one of the solutions

% the data
     fnam='foo.txt'; % <- your file with above contents...
     ntxt={
          'steve 42'
          'john 23'
          'adam'
          'ben'
     };
% the engine
     s=textread(fnam,'%s',...
               'delimiter','\n','whitespace','');
     s=[s(1:end-1);ntxt];
     dlmwrite(fnam,char(s),...
               'delimiter','');
% the result
     type(fnam);
%{
     John Jenny
     David 23
     Peter 35
     Mark
     steve 42
     john 23
     adam
     ben
%}

us

Subject: textread

From: Jessica

Date: 3 Jun, 2008 01:27:01

Message: 5 of 11

"us " <us@neurol.unizh.ch> wrote in message <g222e9
$2jd$1@fred.mathworks.com>...
> "Jessica ":
> <SNIP appending boring data to a file...
>
> > John Jenny
> > David 23
> > Peter 35
> > Mark
> > David Sue
>
> one of the solutions
>
> % the data
> fnam='foo.txt'; % <- your file with above contents...
> ntxt={
> 'steve 42'
> 'john 23'
> 'adam'
> 'ben'
> };
> % the engine
> s=textread(fnam,'%s',...
> 'delimiter','\n','whitespace','');
> s=[s(1:end-1);ntxt];
> dlmwrite(fnam,char(s),...
> 'delimiter','');
> % the result
> type(fnam);
> %{
> John Jenny
> David 23
> Peter 35
> Mark
> steve 42
> john 23
> adam
> ben
> %}
>
> us
>

Thanks, this helped alot. The only problem remaining is
that, when I open the final .txt files directly, the values
are listed horizontally and not vertically:

Results:

John Jenny David 23 Peter 35...

And not

John Jenny
David 23
Peter 35

Any suggestions about changing the orientation?

Subject: textread

From: us

Date: 3 Jun, 2008 05:46:02

Message: 6 of 11

"Jessica ":
<SNIP (probably) missing CRLF...

> The only problem remaining is that, when I open the
> final .txt files directly, the values
> are listed horizontally and not vertically...

do you work in a wintel system with files created on a unix
system?

us

Subject: textread

From: Jessica

Date: 3 Jun, 2008 11:26:02

Message: 7 of 11

>
> do you work in a wintel system with files created on a unix
> system?
>
> us


Hi,

I am working on a Windows PC running Vista business on a laptop.

Subject: textread

From: Jessica

Date: 3 Jun, 2008 11:35:09

Message: 8 of 11

With an Intel(R) Core(TM)2 CPU

Subject: textread

From: us

Date: 3 Jun, 2008 14:28:02

Message: 9 of 11

"Jessica ":
<SNIP file reading conundrum...

> I am working on a Windows PC...

how does it look if you open the file in notepad (without
the format/wrap option turned on)...
us

Subject: textread

From: Jessica

Date: 3 Jun, 2008 22:37:01

Message: 10 of 11

"us " <us@neurol.unizh.ch> wrote in message <g23kdi$r45
$1@fred.mathworks.com>...
> "Jessica ":
> <SNIP file reading conundrum...
>
> > I am working on a Windows PC...
>
> how does it look if you open the file in notepad (without
> the format/wrap option turned on)...
> us

I turned the wrap function off and it looks better.
However, the lines are still not perfectly even, such that
the result looks like:

John 34
   Jenny 3
 Sarah 2

Also, this causes a problem when I load the new .txt file
into another code that expects the .txt file to be a list.
I get the error:

Trouble reading floating point number from file (row 6,
field 1) ==> John 34 \n

Subject: textread

From: Jessica

Date: 3 Jun, 2008 23:31:08

Message: 11 of 11

"Jessica " <jyorzinski@ucdavis.edu> wrote in message
<g24h2d$l0i$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <g23kdi$r45
> $1@fred.mathworks.com>...
> > "Jessica ":
> > <SNIP file reading conundrum...
> >
> > > I am working on a Windows PC...
> >
> > how does it look if you open the file in notepad
(without
> > the format/wrap option turned on)...
> > us
>
> I turned the wrap function off and it looks better.
> However, the lines are still not perfectly even, such
that
> the result looks like:
>
> John 34
> Jenny 3
> Sarah 2
>
> Also, this causes a problem when I load the new .txt file
> into another code that expects the .txt file to be a
list.
> I get the error:
>
> Trouble reading floating point number from file (row 6,
> field 1) ==> John 34 \n

You can disregard the problem with the floating point error
(because I figured out what I was doing incorrectly).
However, the text is still not perfectly aligned.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us