Add a new array to a strucure

A simple and quick question.
I have a structure A and I want to add a new array to it and the array is
X = [365 322 481 727 914 605 785 313 806 821]
I tried this
A(i).Rim = X';
But it puts all the numbers in the last row of the structure, and i want to put one number in each row. What should i do, any suggestion?

 Accepted Answer

Stephen23
Stephen23 on 24 May 2017
Edited: Stephen23 on 24 May 2017
Taking a guess that you have a non-scalar structure and that you want to allocate new data to the same field of each element of the structure, then this is trivial using a comma-separated list:
>> X = [365,322,481,727,914,605,785,313,806,821];
>> C = num2cell(X);
>> A = struct('Z',num2cell(1:numel(X))'); % fake structure
>> [A.Rim] = C{:};
You can also use indexing to allocate to the correct elements of A.

More Answers (0)

Categories

Tags

Asked:

on 24 May 2017

Commented:

on 24 May 2017

Community Treasure Hunt

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

Start Hunting!