saving matlab results

6 views (last 30 days)
Win Thomas Halim
Win Thomas Halim on 4 May 2011
hi there, anyone know hot to save results to a file in matlab? suppose i had this x,y,z data that i want to save it as results.str that is openable via matlab. anyone can help? thank you very much

Accepted Answer

Jan
Jan on 4 May 2011
"Put z inthe first column" is not an exact definition of the output. Do you want to write an ASCII file?
FID = fopen(FileName, 'w');
if FID < 0, error(Cannot open file); end
data = [z, y, x];
fprintf(FID, '%g %g %g\n', transpose(data));
fclose(FID);
See "help fprintf", if you want to specify another numerical format, e.g. more digits after the dot.
  2 Comments
Win Thomas Halim
Win Thomas Halim on 4 May 2011
hi there,
thank you for your help,
but i have a question here, at first you type:
FID=fopen(FileName,'w')
does that command ask matlab to open a file?
because here in this case i want to write a new file, not edit it. a friend of mine told me to use 'fwrite' command but i think it will not solve this case.
Jan
Jan on 4 May 2011
@Win: Simply read "help fopen" to see, what this command does: " 'w' open file for writing; discard existing contents".
FWRITE writes binary data, FPRINTF writes ASCII data. Both could solve your problem, it only depends on your needs. If the file need not be readable by human, a binary format is more efficient. How is the ".str" format defined?!

Sign in to comment.

More Answers (1)

Andreas Goser
Andreas Goser on 4 May 2011
From your description, I'd say you have x, y, z in your MATLAB workspace. Then you can save the data with the SAVE command. The default is a so called MAT file, but you can also choose the ASCII format. See the documentation for that.
There are other ways to export. The documenation is pretty good.
  1 Comment
Win Thomas Halim
Win Thomas Halim on 4 May 2011
thank you, but my problem here, I don't want to save the entire variables in my Matlab workspace,
here's the detail of my problem:
after some calculations, i had this x,y,z data (size 100x1 each) to be proceed into another function that read only ".str" file in order to simulate a nanomachining structures.
i want to put z in the first column, x in the 2nd column and y in the third column as results.str.
is it possible to do that?
thank you very much

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!