Adding fields of two different structs together
10 次查看(过去 30 天)
显示 更早的评论
% Example structures:
part(1) = struct('name', 'x', 'number', 1);
part(2) = struct('name', 'y', 'number', 2);
part(3) = struct('name', 'z', 'number', 3);
% Example: 1st user input should be the 'name' so "x"
% 2nd user input should be another'name' so "z"
% total_number = 1 + 3
3 个评论
Stephen23
2023-5-13
" is there a way to make it depend on the user's input?"
part = struct("x",1,"y",2,"z",3);
part.("x")+part.("z")
采纳的回答
Matt J
2023-5-13
It would be a better to use a dictionary than a struct,
part=dictionary(["x","y","z"],1:3);
part("x")+part("z")
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!