call out matlab structure using for loop
4 views (last 30 days)
I have j structures Ts(j) . And the elements inside the structure are just like T(3) below
can I call out e1 e2 e3 by using for loop?
or I can only call they by using Ts(i).e1 Ts(i).e2 Ts(i).e3.
Because I need to check whether e1||e2||e3 third element is 1 or 2 , and now I have j structures.
If I can't use for loop ↓ ,then I ill have three " if "
Max Alger-Meyer on 9 Mar 2022
You can absolutely use a loop! The trick is to use the 'fieldnames' function as shown below.
SampleStruct.e1 = [129 152 1];
SampleStruct.e2 = [129 164 2];
SampleStruct.e3 = [152 164 1];
names = fieldnames(SampleStruct);
for i = 1:numel(names)