How to save workspace variables and values a s binary file

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.

댓글 수: 4

This can be tricky when you start getting into graphics objects, or cell arrays, or non-scalar struct especially if not all the descendants have the same fields... And how would you like arrays with 3 or more dimensions to be output?
I do not know if I got what you mean correctly but the mentioned Workspace includes just 50 variables and their values. I need to have both variable names and values for the next steps.
Are all of the variables pure numeric? Are they all row vectors? Are some of them 2D arrays? Are some of them 3D arrays?
they are all pure numeric and row vector

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2018년 3월 5일
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

댓글 수: 2

Great, Thanks for the help. There is still one problem. Is there any way to print out the value of either of the variables just right in front of the variables' names to the command window?
Try this:
% 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;
thisValue = eval(thisVariable);
fprintf('%f is the value for %s.\n', thisValue, thisVariable);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2018년 3월 3일

댓글:

2018년 3월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by