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.

17 comments:

Unknown said...

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

srini
ajithnu78@yahoo.com

ndevarak 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?

Anonymous said...

great help, thanks for that!

Anonymous said...

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

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!

Anonymous 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?

Anonymous said...

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

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!

Anonymous said...

Thanks so much

Anonymous said...

so helpful. thank you!!!

Anonymous said...

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

Anonymous said...

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

Aggie said...

Thanks for that! This was very useful.

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!

Unknown said...

thanks for this code Anil!

Anonymous said...

Thanks a lot for this comment. It was a great help. And thanks anonymous for giving the extension

Unknown said...

great! thanks

Moving to a new website

This website was intended to be a little collection of tips and tricks, but I haven't been able to update it as often as I originally in...