call consecutive variables for loop

조회 수: 5 (최근 30일)
Ana
Ana 2013년 4월 17일
Hi,
I have variables in the workspace with the following names: A2010,A2011, A2012.....
Need to to the following for each variable:
AA2010=A2010';
t = AA2010>=2000;
z = [find([true,diff(t)~=0]) numel(AA2010)+1];
idx = [z(1:end-1);z(2:end)-1];
i1 = idx(:,t(idx(1,:)));
i2 = i1(:,(diff(i1) + 1) >= 1);
out = arrayfun(@(x)AA2010(i2(1,x):i2(2,x)),1:size(i2,2),'un',0);
L=length(out);
I'm trying to use:
for i=2010:2037
eval(['A' num2string(i) '=i'])
end
however no results came out...
Any suggestions?
Thanks

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 4월 17일
It's not num2string but num2str. Also it's not a good idea to use eval

Walter Roberson
Walter Roberson 2013년 4월 17일

Jan
Jan 2013년 4월 17일
To mention it the third time: Do not do this. Storing indices inside the names of variables is a really bad idea, as you see already. Using an array instead, or two arrays, where one contains the values and the other the idnices, would be much clearer and easier to handle.

카테고리

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