How to form a structure array variable?

1 vue (au cours des 30 derniers jours)
Leon
Leon le 26 Oct 2020
Commenté : Leon le 26 Oct 2020
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!

Réponse acceptée

Cris LaPierre
Cris LaPierre le 26 Oct 2020
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 commentaire
Leon
Leon le 26 Oct 2020
Exactly what I'm looking for. Many thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

Stephen23
Stephen23 le 26 Oct 2020
Modifié(e) : Stephen23 le 26 Oct 2020
  1 commentaire
Leon
Leon le 26 Oct 2020
Thanks for sharing!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by