How to save workspace variables and values a s binary file

5 vues (au cours des 30 derniers jours)
Mos_bad
Mos_bad le 3 Mar 2018
Commenté : Image Analyst le 5 Mar 2018
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 commentaires
Walter Roberson
Walter Roberson le 5 Mar 2018
Are all of the variables pure numeric? Are they all row vectors? Are some of them 2D arrays? Are some of them 3D arrays?
Mos_bad
Mos_bad le 5 Mar 2018
they are all pure numeric and row vector

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 5 Mar 2018
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 commentaires
Mos_bad
Mos_bad le 5 Mar 2018
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?
Image Analyst
Image Analyst le 5 Mar 2018
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by