数据结构中字段的引用问题。

unction addressStruct(Struct,Field)
if isfield(Struct,Field)
fprintf('The value of the %s field is: ',Field);
disp(Struct.Field);
else
fprintf('Error:%s is not a valid field',Field);
end

 Accepted Answer

sedelbx
sedelbx on 17 May 2023

0 votes

先回去搞清楚,Field 到底是 'code' 这个字符串,还是 code 这个变量名。
S.code = 1;
disp( S.code ) % 正确
Field = 'code';
isfield( S, Field )
disp( S.Field ) % 错误

More Answers (0)

Categories

Find more on App 构建 in Help Center and File Exchange

Asked:

on 17 May 2023

Answered:

on 17 May 2023

Community Treasure Hunt

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

Start Hunting!