how to load saved workspace in a custom named variable?
Show older comments
Hello,
this is what i am currently doing:
for i=1:length(A)
[y1,y2] = myfun(x,A)
savefile = ['test_'num2str(A(i)) '_and_' num2str(x) '.mat'] ;
save(savefile)
end
so i am currently saving all the workspace to a custom .mat file. Now i need to load those workspaces but i need to load them into struct variables that have the same name of the .mat file because i have a lot of data to work with. I tried with these:
for i=1:length(A)
['test_'num2str(A(i)) '_and_' num2str(x)] = load(['test_'num2str(A(i)) '_and_' num2str(x) '.mat']);
end
but it says: "Error: Assigning the function output to this expression is not supported."
how can i fix these problem?
2 Comments
"i need to load them into struct variables that have the same name of the .mat file because i have a lot of data to work with."
That would be about the worst approach.
Don't paint yourself into a corner of writing slow, complex, inefficient code just to access your data:
Davide Maglione
on 20 Oct 2020
Accepted Answer
More Answers (1)
Ameer Hamza
on 20 Oct 2020
Naming variables dynamically is not a good idea: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. Just load it in a struct array or a cell array.
Struct array:
for i=1:length(A)
s(i) = load(['test_'num2str(A(i)) '_and_' num2str(x) '.mat']);
end
or a cell array
C = cell(length(A),1);
for i=1:length(A)
C{i} = load(['test_'num2str(A(i)) '_and_' num2str(x) '.mat']);
end
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!