How can I sava a series of variables to a txt file?

1 view (last 30 days)
Hi all,
I have a huge series of variables in the matlab workspace. Below you'll find an example of how one of these variables looks like. The problem comes when I want to save all of them to a single textfile. The code below works, although I know there must be a more elegant and faster way to do is. Note that some variables are missing (data3, data5 and data6).
Can someone please help me?
data1 = [297.08,29.54,80.75];
fid = fopen('alldata.txt','w');
fprintf(fid,'%d\t%d\t%d\n',data1');
fprintf(fid,'%d\t%d\t%d\n',data2');
fprintf(fid,'%d\t%d\t%d\n',data4');
fprintf(fid,'%d\t%d\t%d\n',data7');

Answers (1)

Walter Roberson
Walter Roberson on 27 Sep 2013
Have you considered save() with the -ascii flag ?
Beyond that, see
  2 Comments
Marty Dutch
Marty Dutch on 27 Sep 2013
Many thanks for your repsonse! I hope you can help me getting this further. I tried save() with the ascii flag as you mentioned although I want to be able to do some text formatting. Now I used the code below but I do not get the right values. alldata.txt contains a huge list containing this (=not the data that is in the variables, data1 contains 297.08,29.54,80.75 ):
103.000000 97.000000 117.000000
115.000000 115.000000 105.000000
97.000000 110.000000 115.000000
This is the code I used:
fid = fopen('alldata.txt','w');
for k=1:300
try
textName = ['data', num2str(k)];
fprintf(fid,'%f\t%f\t%f\n',textName');
catch
continue;
end
end
Marty Dutch
Marty Dutch on 1 Oct 2013
Is there someone who can help me pls with my question above?:) Until this last step is not resolved I cannot run analysis:(

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!