f



How to create a text file having multiple line texts?

How can I create a text file from the following cell variable having characters, not numeric data?
a{1} = '1st one'
a{2} = '2nd one'

In the created text file, I like to have '1st one' in the first line and '2nd one' in the second line.

Thanks in advance
0
Wes
1/25/2011 10:51:03 PM
comp.soft-sys.matlab 211264 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

4 Replies
1008 Views

Similar Articles

[PageSpeed] 44

"Wes" wrote in message <ihnk4n$ncg$1@fred.mathworks.com>...
> How can I create a text file from the following cell variable having characters, not numeric data?
> a{1} = '1st one'
> a{2} = '2nd one'
> 
> In the created text file, I like to have '1st one' in the first line and '2nd one' in the second line.
> 
> Thanks in advance

b=1:10; %How many cells, 10 for example
a = num2cell(b); %put the values into the cell
fid = fopen('exp.txt', 'wt'); %open the file
fprintf(fid,'%dst one\n', cell2mat(a(b))); %write your data to the file
fclose(fid) %close the file
0
Paulo
1/25/2011 11:56:05 PM
Dear Wes,

> How can I create a text file from the following cell variable having characters, not numeric data?
> a{1} = '1st one'
> a{2} = '2nd one'

FID = fopen('FileName.txt', 'w');
fprintf('%s\n', a{:});
fclose(FID);

Kind regards, Jan
0
Jan
1/26/2011 12:17:04 AM
%Had a mistake in the other code
b=1:10; %How many cells, 10 for example
a = num2cell(b); %put the values into the cell
fid = fopen('exp.txt', 'wt'); %open the file
fprintf(fid,'%dst one\n', cell2mat(a(b(1)))); %write first value
fprintf(fid,'%dnd one\n', cell2mat(a(b(2:end)))); %write the rest of values
fclose(fid); %close the file
0
Paulo
1/26/2011 12:19:04 AM
Dear Wes,

Sorry, I forgot a FID in the FPRINTF:
  a{1} = '1st one'
  a{2} = '2nd one'
  FID = fopen('FileName.txt', 'w');
  fprintf(FID, '%s\n', a{:});
  fclose(FID);

If you want DOS linebreaks on a DOS machine, you can either use this:
  FID = fopen('FileName.txt', 'wt')
or this:
  fprintf(FID, ['%s', char([13, 10])], a{:});
But most editors can handle the simple CHAR(10) as line break also.

Kind regards, Jan
  
0
Jan
1/26/2011 12:20:23 AM
Reply: