MATLAB Answers


How can i save multiple variables in a text file, including the variable names

I have a workspace with 30 variables, all sized 8x1.

I would like to save all of them in a .txt file, including their name.

The command I am using is:

save( 'filename.txt' , '-ASCII' );

It saves the variable values as intended, but not the variable names alongside their values.

Any help would be most welcome! Thank you in advance!

  1 Comment

why don' t save them to mat file? is there any reason?

Log in to comment.

1 Answer

Answer by Babak
on 19 Nov 2012
 Accepted Answer

 myfilepath = 'C:\myfile.txt';
 fid = fopen(myfilepath, 'wt');
 fprintf(fid, '% this is the beginning of my file');
 fprintf(fid, 'var1 = %d ',evalin('base',var1));  % if var1 is double

you can use "whos" to find what variables u have in base workspace...


Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Win prizes and improve your MATLAB skills

Play today