MATLAB: How to fill a structure array with a scalar array

arraysMATLABstruct

I have a structure array like this:
myStruct=repmat(struct('field1',0),10,1);
and I have a double array:
myarray = [1 2 3 4 5 6 7 8 9 10];
If I wanto to initialize mystruct with my array I have to do this>
for i = 1:10 mystruct(ii).field1 = myarray(ii); end
There is a way to do this with one row of statement?
If I do
mystruct.field1 = myarray
I get the error:
"Scalar structure required for this assignment"
I've also tried:
mystruct(:).field1 = myarray(:)
but the error was: "Expected one output from a curly brace or dot indexing expression, but there were 10 results."
what can I do?

Best Answer

If you have a non-scalar structure and that you want to allocate new data to the same field of each element of that structure, then this is easiest using a comma-separated list:
>> myStruct=repmat(struct('oldfield',0),10,1);
>> myarray = [1,2,3,4,5,6,7,8,9,10];
>> C = num2cell(myarray);
>> [myStruct.newfield] = C{:};