How to assign a vector data to a structure array?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Walter Roberson
el 2 de Oct. de 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 comentarios
Matt Fig
el 2 de Oct. de 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 comentarios
Ver también
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!