I thought deal should do it but it does not, and I cannot find another nice solution.
I have an array a = 1:2. I would like to put the values 1 and 2 into a structure array b like so:
b(1).a = 1
b(2).a = 2
To my surprise, [b(1:2).a] = deal(1:2) does not deal the values, but puts the vector [1 2] into each field a of structure b:
>> b(1)
ans = 
    a: [1 2]
>> b(2)
ans = 
    a: [1 2]
Am I missing something with syntax here?
deal does what it is expected to do. It distributes the input arguments among the outputs, and if it has only one argument (the vector 1:2) it replicates it as many times as the number of output arguments. You were probably looking for:
[b(1:2).a] = deal(1, 2); %// or simply [b.a] = deal(1, 2)
In the general case, you'll probably have to create a cell array from your values (e.g using num2cell) and use a comma-separated list, for instance:
C = num2cell(v);         %// v stores the values
[b.a] = deal(C{:});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With