Reading variables from workspace
126 views (last 30 days)
Hi- I was wondering if there is a way to read all variables from workspace ? I want to make a .mat file so if I run my simulation its spitting 20 variables in work space. I want to write these in a mat file e.g.
data = [a b c d f r g];
save data.mat data
Is there a way in which program read all variables from workspace?
More Answers (2)
Walter Roberson on 9 Sep 2015
Do not create those variables dynamically in the workspace. Create fields of a structure. You can then save to a .mat file as individual variables using the save -struct option.
If you have a .mat file with a number of variables and you want to work on each of them, then use
datastruct = load('YourFile.mat');
Now each of the variables in the .mat file will appear as fields in datastruct. You can find out what is there using fieldnames(datastruct) . If you want to apply the same operation to each variable you can do that using structfun()
Sulaymon Eshkabilov on 4 Feb 2019
Here is an alternative (simpler) solution to the problem:
% save all variables in the workspace:
% Store the variable names from mydat.mat in a cell: MYvars
MYvars = who('-file', 'MYdata.mat');
for k = 1:length(MYvars)