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:
dlmwrite

Subject: dlmwrite

From: Jessica

Date: 9 Sep, 2008 03:48:02

Message: 1 of 5

Hi,

I'm trying to append words onto a preexisting .txt file. For example, my original a.txt file:

Jo 9
Tom 10
Jon
Tim 12

And I want to append these values:

ntxt={
'Henry 34'
'Bob 3'
'Jim '
}

so that it produces:

Jo 9
Tom 10
Jon
Tim 12
Henry 34
Bob 3
Jim

I have tried the code:

s=textread(a,'%s', 'delimiter','\n','whitespace','');
Final=[s;ntxt];
dlmwrite(fnam,char(Final), 'delimiter', '');

However, this gives me very strange formating. I need the txt file to have things listed like:

Jo 9
Tom 10
Jon
Tim 12
Henry 34
Bob 3
Jim

Any suggestions?

Thanks!

Subject: dlmwrite

From: Shanmugam Kannappan

Date: 9 Sep, 2008 06:02:06

Message: 2 of 5

Hello Sir/Madam,

You can do this operation by the folloing code.

%% This part will do the first operation
fp=fopen('a.txt','w+');
fprintf(fp,'Jo 9\nTom 10\nJon\nTim 12\n')
fclose(fp)
%upto this u will have three rows only.
%% U can append the data's to the existing file(a.txt)
% by this code
fp=fopen('a.txt','a+');
fprintf(fp,'Henry 34\nBob 3\nJim\n')
fclose(fp)
% finaly u will get the txt file with all the strings in
% Order
open('a.txt')

Regards
Shanmugam.K

"Jessica " <jyorzinski@ucdavis.edu> wrote in message <ga4rli$s9v$1@fred.mathworks.com>...
> Hi,
>
> I'm trying to append words onto a preexisting .txt file. For example, my original a.txt file:
>
> Jo 9
> Tom 10
> Jon
> Tim 12
>
> And I want to append these values:
>
> ntxt={
> 'Henry 34'
> 'Bob 3'
> 'Jim '
> }
>
> so that it produces:
>
> Jo 9
> Tom 10
> Jon
> Tim 12
> Henry 34
> Bob 3
> Jim
>
> I have tried the code:
>
> s=textread(a,'%s', 'delimiter','\n','whitespace','');
> Final=[s;ntxt];
> dlmwrite(fnam,char(Final), 'delimiter', '');
>
> However, this gives me very strange formating. I need the txt file to have things listed like:
>
> Jo 9
> Tom 10
> Jon
> Tim 12
> Henry 34
> Bob 3
> Jim
>
> Any suggestions?
>
> Thanks!

Subject: dlmwrite

From: Jessica

Date: 9 Sep, 2008 15:20:19

Message: 3 of 5

Thanks for this advice. Unfortunately, the a.text file I gave was a shortened example for a very long file. Thus, I would not really want to type out everything that is contained in that file. Even if I do type out a shortened version of it, the formatting is still off.

"Shanmugam Kannappan" <shanmugambe@gmail.com> wrote in message <ga53gu$sr7$1@fred.mathworks.com>...
> Hello Sir/Madam,
>
> You can do this operation by the folloing code.
>
> %% This part will do the first operation
> fp=fopen('a.txt','w+');
> fprintf(fp,'Jo 9\nTom 10\nJon\nTim 12\n')
> fclose(fp)
> %upto this u will have three rows only.
> %% U can append the data's to the existing file(a.txt)
> % by this code
> fp=fopen('a.txt','a+');
> fprintf(fp,'Henry 34\nBob 3\nJim\n')
> fclose(fp)
> % finaly u will get the txt file with all the strings in
> % Order
> open('a.txt')
>
> Regards
> Shanmugam.K
>
> "Jessica " <jyorzinski@ucdavis.edu> wrote in message <ga4rli$s9v$1@fred.mathworks.com>...
> > Hi,
> >
> > I'm trying to append words onto a preexisting .txt file. For example, my original a.txt file:
> >
> > Jo 9
> > Tom 10
> > Jon
> > Tim 12
> >
> > And I want to append these values:
> >
> > ntxt={
> > 'Henry 34'
> > 'Bob 3'
> > 'Jim '
> > }
> >
> > so that it produces:
> >
> > Jo 9
> > Tom 10
> > Jon
> > Tim 12
> > Henry 34
> > Bob 3
> > Jim
> >
> > I have tried the code:
> >
> > s=textread(a,'%s', 'delimiter','\n','whitespace','');
> > Final=[s;ntxt];
> > dlmwrite(fnam,char(Final), 'delimiter', '');
> >
> > However, this gives me very strange formating. I need the txt file to have things listed like:
> >
> > Jo 9
> > Tom 10
> > Jon
> > Tim 12
> > Henry 34
> > Bob 3
> > Jim
> >
> > Any suggestions?
> >
> > Thanks!
>

Subject: dlmwrite

From: Gavrilo Bozovic

Date: 9 Sep, 2008 15:42:01

Message: 4 of 5

Hi!

I just tried your matrix concatenation using simply [A;B], and it seemed to work...

could you provide an example of the "strange" formatting it gives you?

"Jessica " <jyorzinski@ucdavis.edu> wrote in message <ga4rli$s9v$1@fred.mathworks.com>...
> Hi,
>
> I'm trying to append words onto a preexisting .txt file. For example, my original a.txt file:
>
> Jo 9
> Tom 10
> Jon
> Tim 12
>
> And I want to append these values:
>
> ntxt={
> 'Henry 34'
> 'Bob 3'
> 'Jim '
> }
>
> so that it produces:
>
> Jo 9
> Tom 10
> Jon
> Tim 12
> Henry 34
> Bob 3
> Jim
>
> I have tried the code:
>
> s=textread(a,'%s', 'delimiter','\n','whitespace','');
> Final=[s;ntxt];
> dlmwrite(fnam,char(Final), 'delimiter', '');
>
> However, this gives me very strange formating. I need the txt file to have things listed like:
>
> Jo 9
> Tom 10
> Jon
> Tim 12
> Henry 34
> Bob 3
> Jim
>
> Any suggestions?
>
> Thanks!

Subject: dlmwrite

From: Gavrilo Bozovic

Date: 9 Sep, 2008 16:00:22

Message: 5 of 5

BTW, if you want to read the file a.txt, 'textread(a,...)' cannot work like this.

you either have to define:

a = 'a.txt'

or write

textread('a.txt',...)

I guess you did either one of these two, otherwise you would have had an execution error.

Tags for this Thread

No tags are associated with 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