Adding fields of two different structs together

조회 수: 8 (최근 30일)
Kono
Kono 2023년 5월 13일
댓글: Stephen23 2023년 5월 13일
% 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
Kono
Kono 2023년 5월 13일
이동: VBBV 2023년 5월 13일
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
Stephen23
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")
ans = 4

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
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")
ans = 4

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by