I want to create a struct in a loop. this is the initial command lines:
for i = 1:n
if condition
myStruct(i).a = i:
myStruct(i).b = 1:
else
myStruct(i) = myFunction(i):
end
end
myFunction is as follow: (real one has more fields)
function myStruct = myFunction(i)
myStruct.a = i;
myStruct.b = 0;
end
when the condition is true for the very first loop (i=1) it works fine, otherwise there is an error with the first assignment as follow:
subscripted assignment between dissimilar structures.
In order to Cope it I did as following but I'm looking for a better more efficient solution.
for i = 1:n
if condition
myStruct(i).a = i:
myStruct(i).b = 1:
else
if i == 1
myStruct = myFunction(i);
else
myStruct(i) = myFunction(i):
end
end
end
but I prefer some shorter solution. I appreciate any suggestion.