Adding fields of two different structs together

1 view (last 30 days)
% 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 Comments
Kono
Kono on 13 May 2023
Moved: VBBV on 13 May 2023
Thanks for the response but is there a way to make it depend on the user's input?
For example I would like the user to input two names, say 'y' and 'z', and then come up with the sum of numbers

Sign in to comment.

Accepted Answer

Matt J
Matt J on 13 May 2023
It would be a better to use a dictionary than a struct,
part=dictionary(["x","y","z"],1:3);
part("x")+part("z")
ans = 4

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!