how to load saved workspace in a custom named variable?

조회 수: 3 (최근 30일)
Davide Maglione
Davide Maglione 2020년 10월 20일
댓글: Davide Maglione 2020년 10월 20일
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
Stephen23
Stephen23 2020년 10월 20일
편집: Stephen23 2020년 10월 20일
"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
Davide Maglione 2020년 10월 20일
i thought that was the best approach! thank you!

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

채택된 답변

Stephen23
Stephen23 2020년 10월 20일
편집: Stephen23 2020년 10월 20일
The efficient approach is to use indexing (rather than anti-pattern dynamic variable names):
It is easy to import the file data into one cell array or one structure, e.g. the same one that dir returns:
D = 'absolute or relative path to the folder where the files are saved';
S = dir(fullfile(D,'*mat'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
S(k).data = load(F);
end
and then you can simply access the data from every file using basic and very efficient indexing, e.g. the third file:
S(3).name
S(3).data
If all of your .mat files contain variables with the same names (which is good data design) then you can also un-nest them by concatenating the data structures them into one non-scalar structure, e.g.:
out = [S.data]
out(3).someVariable
out(3).otherThing
  댓글 수: 3
Stephen23
Stephen23 2020년 10월 20일
You can also use an absolute/relative filename when saving data, e.g.:
D = 'absolute or relative path to the folder where the files are saved';
F = sprintf('test_%d_and_%d.mat',A(i),x);
save(fullfile(D,F))
Davide Maglione
Davide Maglione 2020년 10월 20일
that's great! thank you!

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

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 10월 20일
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

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by