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]};
답변 (2개)
ES
2013년 9월 23일
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
cwc
2013년 9월 23일
ES
2013년 9월 23일
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
2013년 9월 23일
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
2013년 9월 23일
@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
2013년 9월 23일
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
2013년 9월 23일
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
cwc
2013년 9월 23일
Jan
2013년 9월 23일
@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
2013년 9월 23일
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.
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!