How to rename variables in a loop?

조회 수: 5 (최근 30일)
Michael
Michael 2014년 6월 16일
답변: Jos (10584) 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에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by