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!