MATLAB Answers

Rita
0

How to load a variable from a loop name mat ?

Asked by Rita
on 6 Aug 2015
Latest activity Answered by Rita
on 6 Aug 2015
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

  0 Comments

Sign in to comment.

2 Answers

Answer by James Tursa
on 6 Aug 2015
Edited by James Tursa
on 6 Aug 2015
 Accepted Answer

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.

  0 Comments

Sign in to comment.


Answer by Rita
on 6 Aug 2015

Thanks a lot James.

  0 Comments

Sign in to comment.