How to assign a value to a cell nested in another cell
조회 수: 1 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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개)
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
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 Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!