How to assign a vector data to a structure array?
13 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Walter Roberson
2012-10-2
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 个评论
Matt Fig
2012-10-2
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!