Is it possible to iterate through a MATLAB structure numerically like a vector instead of using the field names?
Simply, I am trying to do the following inside an EML block for Simulink:
S.a.type = 1;
S.a.val = 100;
S.a.somevar = 123;
S.b.type = 2;
S.b.val = 200;
S.b.somevar2 = 234;
S.c.type = 3;
S.c.val = 300;
S.c.somevar3 = 345;
for i = 1:length(s)
itemType = S(i).type;
switch itemType
case 1
val = S(i).val * S(i).somevar1;
case 2
val = S(i).val * S(i).somevar2;
case 3
val = S(i).val * S(i).somevar3;
otherwise
val = 0
end
end
disp(var);
You need to use the field names, but you can do so dynamically. If you have a structure defined as:
Then you can access the field
field1with eitherThe only thing you need is the function
fieldnamesto dynamically get the field names such that your code example will look somewhat likeIf those are the exact field names, you should do some other things. First of all you would need to rethink your names and secondly you could use part of Matt’s solution to simplify your code.