Cell array and cell structure

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]};

Antworten (2)

ES
ES am 23 Sep. 2013

1 Stimme

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 Kommentare

cwc
cwc am 23 Sep. 2013
But to my knowledge, doesn't Matlab works in such a way that if I would want to access the weight of Oxygen then I would have to write it as W{1,1} such that it denotes the position of the specific element in the broad category of elements and (1,2) to imply the position of the weight within a cell element?
ES
ES am 23 Sep. 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};
Jan
Jan am 23 Sep. 2013
Bearbeitet: Jan am 23 Sep. 2013
@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
Image Analyst am 23 Sep. 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
Jan am 23 Sep. 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
Image Analyst am 23 Sep. 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?

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 23 Sep. 2013

0 Stimmen

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 Kommentare

cwc
cwc am 23 Sep. 2013
How do I link the s(1)....s(4) such that it complies with function [W S ] = generate array()? It's giving me a "reference to non-existent field" whenever I do a "S(1).C"
function [W, S] = generate_array()
W = {'Oxygen',15.9994 ; 'Carbon' , 12.011 ;'Nitrogen' , 14.00674 ; 'Sulfur', 32.066 ; 'Hydrogen' ,1.00794}; S = [S(1);S(2);S(3);S(4)];
S(1).name = 'Isoleucine'; S(1).C = 6; S(1).H = 13; S(1).N = 1; S(1).O = 2; S(1).S = 0;
S(2).name = 'Lysine'; S(2).C = 6; S(2).H = 14; S(2).N = 2; S(2).O = 2; S(2).S = 0;
S(3).name = 'Methionine'; S(3).C = 5; S(3).H = 11; S(3).N = 1; S(3).O = 2; S(3).S = 1;
S(4).name = 'Phenylalanine'; S(4).C = 9; S(4).H = 11; S(4).N = 1; S(4).O = 2; S(4).S = 0;
Jan
Jan am 23 Sep. 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
Image Analyst am 23 Sep. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Hilfe-Center und File Exchange

Gefragt:

cwc
am 23 Sep. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by