append data to the end of a txt file...works for 1 line...it doesnt work for two lines

29 views (last 30 days)
the above append alength to the end of 111.txt
clc
fid = fopen('C:\Users\Mr Andrew\Desktop\111.txt', 'a');
for k = 1:5
fprintf(fid,'alength \r\n')
end
fclose(fid);
but if i try this
clc
fid = fopen('C:\Users\Mr Andrew\Desktop\111.vbs', 'a');
for k = 1:5
fprintf(fid,'alength
blength \r\n')
end
fclose(fid);
it doesnt work...what i am doing wrong? thank you...

Accepted Answer

Cedric
Cedric on 26 Aug 2013
fprintf(fid,'alength\r\n blength\r\n') ;

More Answers (1)

Madhura Suresh
Madhura Suresh on 26 Aug 2013
Use '\n' to add a new line before writing 'blength'
clc
fid = fopen('C:\Users\Mr Andrew\Desktop\111.txt', 'a');
for k = 1:5
fprintf(fid,'alength\n blength \r\n')
end
fclose(fid);
  2 Comments
Andrew
Andrew on 26 Aug 2013
with '\n'
the txt will be like
alength blength
alength blength
alength blength
alength blength
alength blength
but i want to be like
alength
blength
alength
blength
alength
blength
alength
blength
alength
blength
Madhura Suresh
Madhura Suresh on 26 Aug 2013
Edited: Madhura Suresh on 26 Aug 2013
No, the '\n' between 'alength' and 'blength' will move the pointer to the next line. My output was:
alength
blength
alength
blength
alength
blength
alength
blength
alength
blength

Sign in to comment.

Categories

Find more on Denoising and Compression in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!