How to assign a value to a cell nested in another cell
4 次查看(过去 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!