How to assign a value to a cell nested in another cell

3 ビュー (過去 30 日間)
Thang  Le
Thang Le 2014 年 4 月 23 日
コメント済み: Thang Le 2014 年 4 月 23 日
Hi everyone,
I have a seemingly simple problem but can't seem to figure out. I have something like this P={'Face1','Face2','Scene1','Scene2'}. What I would like to do is to assign a value to each of the element in P and still preserve the element's identity. For instance, the values could be assigned like this: Face1=4*5, Face2=23*6, etc.
However, when I do P{1,1}=4*5 then P becomes {20,'Face2','Scene1','Scene2}.This is not what I want since I've lost Face1. I'd like to keep the identity of each cell so when I call D{1,1} I'll get Face1, and when I call Face1, I'll get 20.
I hope the question is clear. Does anyone have any suggestions?
Thanks!

採用された回答

Jan
Jan 2014 年 4 月 23 日
This seems to be a magic question. What about this:
P = {'Face1','Face2','Scene1','Scene2'}
Face1 = 4*5
Or perhaps you want to use the string dynamically. Then using dynamic field names with structs is neat:
S.(P{1,1}) = 4*5;
disp(S)
S.Face1
  1 件のコメント
Thang  Le
Thang Le 2014 年 4 月 23 日
Thank you, Jan. This is what I was looking for.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 4 月 23 日
I don't understand why you want this. Why do you? After all, the row and column are already numbers that uniquely identify every single element in your cell array . If you want just one number you could use sub2ind(). But I still don't know why .
  2 件のコメント
Thang  Le
Thang Le 2014 年 4 月 23 日
My rationale for doing this is that the actual P has 72 elements. Also depending on the version of the experiment I have, the order of these elements may change from one subject to another. So I thought I could specify the order of the element with P={'Face1','Face2','Scene1' etc.} for each version. Next, I'd do the caculation later for each of the elements. And since there are 72 elements, I thought a loop might be appropriate. Thus, instead of specify Face1=4*5, Face2=etc. I could do something like this:
for i=1:length(P)
P{i,1}= whatever calculation is to be done
end
At the end, I'd like to get the mean of all the Face elements, mean of all the Scene elements.
Image Analyst
Image Analyst 2014 年 4 月 23 日
Not sure I understand quite yet, but let me ask this first: Do you want P to be a 1-D array of cells, or a 2-D matrix of cells? You seem to have used it both ways.
And regarding your subject line about "a cell nested in another cell", does any of the cells in the array contain another cell ? Because to me, that's what nested means. If so, what's in the nested cell. Or do all the cells simply contain single, simple strings (character arrays)?
Have you read the FAQ on cells? http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F I think it gives a good intuitive explanation of cells and cell arrays.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by