problem with code
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 22 de Oct. de 2011
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 comentario
Jan
el 22 de Oct. de 2011
+1: Best answer. Perhaps ignoring your answers is a running joke. However, I'm missing the punchline.
Categorías
Más información sobre Function Creation 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!