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:
thanks for your tip, but can i write it a double-tabs
srini
ajithnu78@yahoo.com
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?
great help, thanks for that!
Cheers Dude!!!!
Thanks a lot for this help.
-
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!
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?
Thank you. This works for me. Next question is how do you dynamically load the files?
Hey there,
you did a great job in writing this little post. I´ve searched for a solution for hours and hours.
Thanks man!
Thanks so much
so helpful. thank you!!!
Thanks! Needed to import files in an array, and this adapted nicely. Thanks for sharing.
I have been looking all over for how to do this. Thanks for posting.
Cheers,
Anne in San Francisco
Thanks for that! This was very useful.
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!
thanks for this code Anil!
Thanks a lot for this comment. It was a great help. And thanks anonymous for giving the extension
great! thanks
Post a Comment