How to assign a vector data to a structure array?
14 views (last 30 days)
Show older comments
I'm trying to assign a vector, x, to a structure element, y.real. Here's the code.
x = [0 1 2 3 4 5]
x = num2cell(x)
[y(1:length(x)).real] = deal(x{:})
I'm trying to understand why I need the [] on the y.real to change this back to an array. If the output of deal() is multiple outputs, each being an element of x, and each element in y.real is a separate cell, why wouldn't the following suffice:
y(1:length(x)).real = deal(x{:})
0 Comments
Answers (2)
Walter Roberson
on 2 Oct 2012
Just an arbitrary syntax choice. MATLAB only allows multiple output locations if they are in [] on the left-hand side. Mathworks could have chosen differently. It was probably easier to code for the way that was chosen.
0 Comments
Matt Fig
on 2 Oct 2012
When you do this:
y(1:length(x)).real = deal(x{:})
MATLAB only sees one output argument, but numel(x) input arguments. This has to do with the way MATLAB performs comma separated list expansion of the arguments. Look at this:
x = num2cell(1:3);
[y(1).real,y(2).real,y(3).real] = deal(x{:});
Now this way should make clear that deal has 3 outputs and 3 inputs. But the LHS above is the same as the LHS when DEAL is used this way:
[y(1:3).real] = deal(x{:});
0 Comments
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!