MATLAB: How to save workspace variables and values a s binary file

printable variables and valuessaveworkspace

I wrote a code and want to save all workspace's variables and their values as a .txt file or other formats as a binary file just to print them out. Note I need both variable names and values.

Best Answer

Try this and see if it does what you want:
% Get list of all variables in current workspace.
s = whos
% Print out variables one by one to the command window.
% Essentially we're using eval() to put each variable on the command line.
% When you do that, MATLAB will report the value to the command line,
% just as if you had manually typed the variable name yourself on the command line.
for k = 1 : length(s)
thisVariable = s(k).name;
eval(thisVariable)
end