I am trying a script that does not work. First I define a structure in matlab:
>> package=struct('item_no',123,'cost',19.99,'price',39.95,'code','g')
package =
item_no: 123
cost: 19.9900
price: 39.9500
code: 'g'
then I have made a script who will try to find out the value of a field in the structure:
inputfield=input('Which field would you like to see: ','s')
if isfield(package, inputfield)
fprintf('The value of the %s field is: %c\n',inputfield,...
eval(['package.'inputfield]))
else
fprintf('Error: %s is not valid field\n', inputfield)
end
but i get error message:
>> structurevalue ??? Error: File: structurevalue.m Line: 6 Column: 22 Unexpected MATLAB expression.

 採用された回答

Mitch Martelli
Mitch Martelli 2011 年 10 月 22 日

0 投票

the problem is in the eval,try to put a blank after 'package.' :
eval(['package.' inputfield])

5 件のコメント

Tor Fredrik Hove
Tor Fredrik Hove 2011 年 10 月 22 日
Thanks for both answers!
I have added a space and it works except one problem. When I want value for item_no it does not give right information:
>> package=struct('item_no',123,'cost',19.99,'price',39.95,'code','g')
package =
item_no: 123
cost: 19.9900
price: 39.9500
code: 'g'
>> structurevalue
Which field would you like to see: cost
inputfield =
cost
The value of the cost field is: 1.999000e+001
>> structurevalue
Which field would you like to see: item_no
inputfield =
item_no
The value of the item_no field is: {
>>
the script again with only an extra space added:
inputfield=input('Which field would you like to see: ','s')
if isfield(package, inputfield)
fprintf('The value of the %s field is: %c\n',inputfield,...
eval(['package.' inputfield]))
else
fprintf('Error: %s is not valid field\n', inputfield)
end
Mitch Martelli
Mitch Martelli 2011 年 10 月 22 日
you have right...
eval(package.item_no)
return the corresponding ASCII symbol because the argoument is integer
Image Analyst
Image Analyst 2011 年 10 月 22 日
I'm not sure why you accepted this answer instead of my answer, or why Mitch even offered this answer, since my answer was posted previously here. I believe my answer is the preferred method, as I often see other experts recommend for dynamic field names and against eval. If you still plan on using eval, you should read the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_it_advised_to_avoid_using_the_.22eval.22_function.3F
Tor Fredrik Hove
Tor Fredrik Hove 2011 年 10 月 22 日
Hi Image Analyst! I am a bit new with matlab so convenient choices is not my strong side. Eval is also a part of the curriculum so I have to know something about it either way
Jan
Jan 2011 年 10 月 22 日
I do not understand what "is part of the curriculum" means. Using EVAL is a bad programming practize, because it is prone to errors and results in slow code.
"package.(inputfield)" is much safer and faster than "eval(['package.' inputfield])".

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2011 年 10 月 22 日

1 投票

This is what the line should look like:
fprintf('The value of the %s field is: %c\n',inputfield, package.(inputfield));
Look up "dynamic field names" in the help for more info.

1 件のコメント

Jan
Jan 2011 年 10 月 22 日
+1: Best answer. Perhaps ignoring your answers is a running joke. However, I'm missing the punchline.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Characters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by