Cell array and cell structure
Mostrar comentarios más antiguos
I am suppose to insert the weight data of an element into the element itself. The funny thing is that W(1,1)(1,2) brings up "x" instead of the value 15.9994. Something is particularly weird here. Leg up?
W = {['Oxygen',15.9994], ['Carbon',12.011],['Nitrogen',14.00674],['Sulfur',32.066],['Hydrogen',1.00794]};
Respuestas (2)
ES
el 23 de Sept. de 2013
Hi cwc, Nothing is Weird. According to MATLAB W is a cell array. and ['Oxygen',15.9994] is the first member of the cell array. As you might know, MATLAB treats ['Oxygen',15.9994] as a string. so W{1} is Oxygen_ where _is a string equivalent of 15.9994.
Ideal thing for you to do is to keep W as
W = {'Oxygen',15.9994, 'Carbon',12.011,'Nitrogen',14.00674,'Sulfur',32.066,'Hydrogen',1.00794};
so u can access Oxygen as W{1}, and 15.9994 as W{2}.
6 comentarios
cwc
el 23 de Sept. de 2013
ES
el 23 de Sept. de 2013
It would if you had given it in this way,
W = {'Oxygen',15.9994; 'Carbon',12.011;'Nitrogen',14.00674;'Sulfur',32.066;,'Hydrogen',1.00794};
@cwc: Lukily we do not have to discuss opinions about how Matlab works. But we can paste Elangovan's code to an M-file and let Matlab decide by its own, if the code is fine.
I do trust Elangovan's suggestion and the bug in ['Oxygen',15.9994] has been explained clearly.
@Elangovan: Please let me know, if I have to write "he" or "she" and if this is the first or family name. I'm not familiar with this name. Thanks.
You need W{1,2} to get the value, while W{2} is W{2,1}.
Image Analyst
el 23 de Sept. de 2013
Good thing you have the photo because in the US we could say the same thing about "Jan" - which is overwhelmingly female in the US.
Jan
el 23 de Sept. de 2013
@Image Analyst: I've seen the beard on Elangovan avatar. But is this enough to say "he"? What might happen, if I replace my photo with one, on which I wear two cute pigtails?
Image Analyst
el 23 de Sept. de 2013
I don't know what might happen. Every year or so you get adventurous with your profile photo, so why not give it a try?
Image Analyst
el 23 de Sept. de 2013
Yes, that is definitely weird. You should have gotten a syntax error when you tried to use W(1,1)(1,2).
Now, W{1,1}(1,2) would work (note the braces instead of parentheses). It would look at the contents of cell (1,1) - which is the string 'Oxygen' - and then at the (1,2) element of that string, which would be 'x'. Or if you wanted the 15.994 number you could do this:
W = {'Oxygen',15.9994; 'Carbon',12.011;'Nitrogen',14.00674;'Sulfur',32.066;,'Hydrogen',1.00794}
result = W{1,2}
as Elangovan said. I think that you could benefit from reading the FAQ on cells: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
3 comentarios
cwc
el 23 de Sept. de 2013
Jan
el 23 de Sept. de 2013
@cwc: S is not defined before the line "S = [S(1);S(2);S(3);S(4)];". Therefore this line must fail. Perhaps you want to omit this line completely? If not, what do you assume this line to do?
Image Analyst
el 23 de Sept. de 2013
As far as I can tell, he wants a structure S. Well, the last few lines define that S, so the S=[....] line is not needed anywhere in the function, either before or after your definition of S. It's just not needed at all. The rest of what you have there is sufficient to create S with the proper members and values.
Categorías
Más información sobre Performance and Memory en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!