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.

15 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.
    -

     
  • At 1:02 PM, Anonymous Anonymous said…

    Thanks so much! I searched and searched and finally stumbled upon your page -- you've just enabled me to run batch jobs the way I need to!

     
  • At 3:08 AM, Anonymous janu said…

    if we save data as ascii file it has minimum of 8 digits, how can i reduce no of digits of that data for futher applications?

     
  • At 6:25 PM, Anonymous Anonymous said…

    Thank you. This works for me. Next question is how do you dynamically load the files?

     
  • At 3:44 AM, Blogger Kjellski said…

    Hey there,

    you did a great job in writing this little post. I´ve searched for a solution for hours and hours.

    Thanks man!

     
  • At 1:56 PM, Anonymous Anonymous said…

    Thanks so much

     
  • At 2:36 PM, Anonymous Anonymous said…

    so helpful. thank you!!!

     
  • At 11:11 PM, Anonymous Anonymous said…

    Thanks! Needed to import files in an array, and this adapted nicely. Thanks for sharing.

     
  • At 6:17 PM, Anonymous Anonymous said…

    I have been looking all over for how to do this. Thanks for posting.
    Cheers,
    Anne in San Francisco

     
  • At 6:08 PM, Blogger Aggie said…

    Thanks for that! This was very useful.

     
  • At 5:48 PM, Anonymous Anonymous said…

    Neat post. Just wanted to add for those people trying to save multiple columns as an ascii here is an addendum, you can use fprintf instead of save. fprintf provides more precise control on number of digits and such.

    use fid=fopen(fOut, 'w'); OR
    fid=fopen(fOut, 'a+') ;
    fprintf(fid, '%f \t %f \n', datvar1 datvar2);

    This is if datvar1 & datvar2 are 2 variables (column matrices). See the documentation for fprintf for clarity and modifications.

    Cheers!

     
  • At 10:29 AM, Blogger Charudatta said…

    thanks for this code Anil!

     

Post a Comment

<< Home