How to rename variables in a loop?
5 views (last 30 days)
Show older comments
Hello,
I want to load 254 files with the name 'TJJ_arc001' etc. Then I want to concatenate them together so that I go from each individual file (723 x 3127) to (723 x 3127 x 254). I have started below but I keep getting errors with the eval line...can someone help me please?
clc
clear
close
%load('/work/uo0122/u253082/TPJJ_updated/TJJ.mat')
real = ones(723,3127,254);
for arc = 1:10%254
arcs = make_string100(arc);
load('/work/uo0122/u253082/TPJJ_updated/TJJ.mat',['TJJ_arc',arcs]);
eval([ 'TJJ_arc' arcs ]) = m(arc);
end
thanks, Michael
0 Comments
Answers (2)
David Sanchez
on 16 Jun 2014
you are not using eval in the right way.
You should do:
x=3;
eval('a = x')
a =
3
Adapt the code to your need, but take a look at eval documentation to grasp a better idea on how to use the function.
doc eval
0 Comments
Jos (10584)
on 16 Jun 2014
DO NOT DO THIS! The usefulness of variables is that their contents change during execution of a piece of code, not their names …
You could use cells for this or fields in a structure
However, I do not understand your problem completely. You say you have 254 files, but your code suggest you only have a single mat file (TJJ.mat) from which you load individual variables that have a name like TJJ_arcNNN (with N being a number)? And what has the variable m to with this?
Here is some piece of code, that may help you
S = load('TJJ.mat') ; % all variables within this file are loaded in the structure S
FN = fieldnames(S)
for k = 1:numel(FN)
tmp = S.(FN{k}) ; % access them one by one
% . . .
end
0 Comments
See Also
Categories
Find more on Variables 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!