How to assign a vector data to a structure array?
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Walter Roberson
le 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 commentaires
Matt Fig
le 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 commentaires
Voir également
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!