How to assign a value to a cell nested in another cell
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Jan
le 23 Avr 2014
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
Plus de réponses (1)
Image Analyst
le 23 Avr 2014
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 commentaires
Image Analyst
le 23 Avr 2014
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.
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!