How to form a structure array variable?

조회 수: 1 (최근 30일)
Leon
Leon 2020년 10월 26일
댓글: Leon 2020년 10월 26일
I have a structure array like the below
Test{1}.a = [1; 2; 3];
Test{1}.b = [7; 3; 22];
Test{1}.c = [4; 9; 22];
...
If I have a variable (Var) that can be either a, or b, or c, ...
If Ind == 1;
Var = 'a';
elseif Ind == 2
Var = 'b';
elseif Ind == 3
Var = 'c';
end
How do I specify Test{1}.Var, so that when Ind == 1, it will be Test{1}.a, and so on?
Many thanks!

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 10월 26일
See Steven Lord's reply here. Basically, something like this:
Test{1}.a = [1; 2; 3];
Test{1}.b = [7; 3; 22];
Test{1}.c = [4; 9; 22];
Ind = 2;
if Ind == 1
Var = {'a'};
elseif Ind == 2
Var = {'b'};
elseif Ind == 3
Var = {'c'};
end
Test{1}.(Var{1})
ans = 3×1
7 3 22
  댓글 수: 1
Leon
Leon 2020년 10월 26일
Exactly what I'm looking for. Many thanks!

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

추가 답변 (1개)

Stephen23
Stephen23 2020년 10월 26일
편집: Stephen23 2020년 10월 26일

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by