How to rename variables in a loop?

3 次查看(过去 30 天)
Michael
Michael 2014-6-16
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

回答(2 个)

David Sanchez
David Sanchez 2014-6-16
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

Jos (10584)
Jos (10584) 2014-6-16
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

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by