MATLAB Tips and Tricks

Friday, December 24, 2004

Saving data using string filenames in MATLAB

It is an easy matter to save data as a mat(MATLAB binary) file or an ASCII file so long as you know the filename beforehand. For ex: to save data in the variable datvar to a file mydata.dat, all you have to do is say
save mydata.dat datvar
However if you want to dynamically generate your filename, things get a little tricky since MATLAB's documentation is sparse about this. Here's how:
% Create a filename (myfile.001) using sprintf
fOut = sprintf('myfile.%03d',i);   % say, i=1

% save data as an ASCII file
save(fOut, 'datvar', '-ascii');

% Or, create a mat file - (myfile001.mat)
% since MATLAB binary files typically have the extension .mat
fOut = sprintf('myfile%03d.mat',i);   % say, i=1

% save data as matlab binary
save(fOut, 'datvar');

If you have multiple columns of data, just ensure that datvar is an array. Remember that the .mat file will retain the name of the variable datvar but the ASCII file will only contain data.

4 Comments:

  • At 5:26 PM, Blogger srinivasan said…

    thanks for your tip, but can i write it a double-tabs

    srini
    ajithnu78@yahoo.com

     
  • At 9:55 AM, Blogger Nagini said…

    Hi,
    I have some 84,000 4x4 matrices generated by a program in matlab. Obviously Matlab does not allow me to view all of those. How do I see/printout/save all of those?

     
  • At 7:12 PM, Anonymous Anonymous said…

    great help, thanks for that!

     
  • At 5:46 AM, Anonymous Anonymous said…

    Cheers Dude!!!!
    Thanks a lot for this help.
    -

     

Post a Comment

<< Home