strings and numbers for a numeric array name

4 ビュー (過去 30 日間)
yasmine
yasmine 2011 年 12 月 15 日
I am using 6 numeric arrays in my project, their names are: block1, block2 , block3 , block4, block5 , block6
i execute the same function for all of them, i need to make this in a for loop, but i failed to index the elements of the arrays, i tried this:
for i=1:6
h = ['block' int2str(i)];
eval(h)
end
for row=1:BRows
for col=1:BCols
if (h(row,col) >= 316 && h(row,col) <= 20)
h(row,col) = 0;
elseif (h(row,col) >= 21 && h(row,col) <= 40)
h(row,col) = 1;
end
end
Would you help me please
  3 件のコメント
yasmine
yasmine 2011 年 12 月 16 日
i tried it but this error occurred:
??? Error: File: fg6.m Line: 86 Column: 40
The expression to the left of the equals sign is not a valid target for an assignment.
any other suggestions?
Jan
Jan 2011 年 12 月 16 日
The problem is, that your "h" is *not* the contents of block1, but the string 'block1'. Avoid EVAL.

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2011 年 12 月 15 日
Rather than doing it like that, I strong suggest you used cell arrays, as described in Doug's answer here: http://www.mathworks.com/matlabcentral/answers/143-how-do-i-make-a-series-of-variables-a1-a2-a3-a10
  1 件のコメント
yasmine
yasmine 2011 年 12 月 16 日
actually i have a problem with cell arrays in my project, i can't use them

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2011 年 12 月 16 日
Please explain why you cannot use cell arrays. "eval([block, int2str(i)])" is such an ugly approach, prone to errors and it has remarkable drawbacks for the processing speed in Matlab. I cannot imaging why "h=block{i}" should be worse. Embedding an index in the name of a variable is not smart. Better use an index as index.
Btw. you do not want to do "eval(h)", but:
eval(['h = block', int2str(i)]);
As you see: The EVAL approach is prone to errors!
  6 件のコメント
yasmine
yasmine 2011 年 12 月 30 日
knowing that i want to keep record of all blocks ( not to overwrite on old data)
Jan
Jan 2011 年 12 月 30 日
@yasmine: Please open a new thread for a new question.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by