How to load a variable from a loop name mat ?

조회 수: 3 (최근 30일)
Rita
Rita 2015년 8월 6일
답변: Rita 2015년 8월 6일
Hi, I would like to load a variable(like X) from a dynamic mat file I have this:
for n=1:4
load(['nano',num2str(n),'.mat']) (X);
do some work on the variable
end

채택된 답변

James Tursa
James Tursa 2015년 8월 6일
편집: James Tursa 2015년 8월 6일
Are you simply asking how to load a specific variable? E.g.,
variable_to_load = 'X';
for n=1:4
load(['nano',num2str(n),'.mat'],variable_to_load);
do some work on the variable
end
Another formulation you might consider is the following:
y = load(['nano',num2str(n),'.mat'],variable_to_load);
z = y.(variable_to_load);
all downstream code then uses variable named z
This latter formulation has the advantage that your downstream code can use a variable name (in this case z) that is known to you up front, regardless of the actual variable name you picked to load.

추가 답변 (1개)

Rita
Rita 2015년 8월 6일
Thanks a lot James.

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by