Store answers in multiple variables?

Hello,
I have a variable B which outputs about 100 cells. Assuming N = 4, how do I go about obtaining cell2mat(B(N(1)) in a new variable x_1, cell2mat(B(N(2)) in x_2, and so forth? Currently I am using:
for cnt = 1:N
hold on;
x = cell2mat(B(cnt));
end
Thanks in advance!
-- Jake

댓글 수: 2

Daniel Shub
Daniel Shub 2011년 9월 28일
In general, you probably do not want to be doing this. Why do you want x1, x2, ... It is only going to lead to evil eval statements later.
Jake Bolanski
Jake Bolanski 2011년 10월 1일
On a separate note, is there a function in MatLab that will allow me to detect colors within an image? I have broken down the image into a matrix via the imread() function. How can I go about detecting whether the leaf detected is green or yellow or so on? Thanks in advance.

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

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 28일

0 개 추천

"hold on" is for graphics. It has nothing to do your operation.
Depending on your cell array B, you may not need cell2mat. See this example:
B={rand(2),magic(2),rand(3),magic(3),rand(4),magic(4)};
[x1,x2,x3,x4]=B{2:5}

댓글 수: 4

Jake Bolanski
Jake Bolanski 2011년 10월 1일
Thanks for the reply. Hold on is there because there is a plot - i simply excluded the code from the question.
I think I was somewhat unclear in my question. What I want to do is this:
Say I have N = 10 but I do not know that N is 10 as it changes from 1 to whatever.
I want to make a new variable for every value of N automatically. Hence, I want it such that x1 = 1, x2 = 2, x3 = 3, ... xN = N. Since I do not know what N is, I cannot do [x1,x2,x3,...,xN].
How can I go about this? I assume I need a loop but cannot figure out how to set up one.
Thanks in advance.
Fangjun Jiang
Fangjun Jiang 2011년 10월 1일
Don't do that. That is a really bad programming. You'll find that is a trap. See http://www.mathworks.com/matlabcentral/answers/143-how-do-i-make-a-series-of-variables-a1-a2-a3-a10
Fangjun Jiang
Fangjun Jiang 2011년 10월 1일
And this http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Jake Bolanski
Jake Bolanski 2011년 10월 1일
Wow, very very very helpful! Thanks a bunch! :D

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by