strings and numbers for a numeric array name

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

Walter Roberson
Walter Roberson 2011년 12월 15일
No h(row,col) can possibly be simultaneously >= 316 but <= 20.
Meanwhile, consider:
h(h >= 21 & h <= 40) = 1;
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일

1 개 추천

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일

0 개 추천

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월 16일
am dividing a image into blocks then plot a histogram for every block
so to plot a histogram, i need a numeric array not a cell array and couldn't convert from cell to numeric using cell2mat()
Jan
Jan 2011년 12월 16일
block{1} = rand(2,2);
block{2] = rand{3,4};
disp(block{1}) % <== as you see "block{1}" is a numeric array!
Consider the curly braces. With round parenthesis "block(1)" you get a scalar cell.
Has my Do Not Eval been impressive enough?
the cyclist
the cyclist 2011년 12월 16일
Here is an example of code that uses numeric array embedded in a cell array, and then makes histograms from them.
block = cell(2,2);
for i = 1:4
block{i} = randn(1000,1);
end
figure
for i=1:4
subplot(2,2,i), hist(block{i},25)
end
Think of a cell array as a container than can hold many other type of MATLAB objects. When you use curly brackets, you are accessing the contents of the container, which in this case is numeric arrays.
yasmine
yasmine 2011년 12월 30일
hi
i need to use a database of 100 images each one's size is 256X384. every image is divided into 6 equal blocks(128x128)
and i want to access the pixels in each block.
how can i do this using a for loop or any iterative mean on arrays or cell arrays in matlab
Thank you
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2011년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by