필터 지우기
필터 지우기

Vertical concatenation of structure fields (compact form)

조회 수: 8 (최근 30일)
Massimiliano Zanoli
Massimiliano Zanoli 2020년 10월 8일
댓글: Massimiliano Zanoli 2020년 10월 8일
Hi,
I know there is a similar question but it's not about the compact form.
My question is: why does the following not result in a vertical concatenation? (it also fails for numerical values)
Or, how can I change it to concatenate vertically into ['value_1' ; 'value_2'] ?
Thanks!
>> structure(1, 1).field = 'value_1';
>> structure(2, 1).field = 'value_2';
>> [structure(:, 1).field]
ans =
'value_1value_2'
>>

채택된 답변

Stephen23
Stephen23 2020년 10월 8일
편집: Stephen23 2020년 10월 8일
Your example concatenates horizontally because it is exactly equivalent to doing this:
[structure(1).field,structure(2).field]
which is just shorthand for
horzcat(structure(1).field,structure(2).field)
If you want to concatenate vertically, use either of these
vertcat(structure.field)
cat(1,structure.field)
For more information on how to use comma-separated lists:
  댓글 수: 3
Stephen23
Stephen23 2020년 10월 8일
Comma-separated arrays are equivalent to values separated by commas, not by semi-colons. So this:
structure.field
is equivalent to
structure(1).field,structure(2).field, .. ,structure(end).field
by the very definition of comma-separated lists. There is no equivalent syntax for semi-colon-separated lists.
Massimiliano Zanoli
Massimiliano Zanoli 2020년 10월 8일
I agree, but (:, 1) should indicate vertical arrangement. That [] defaults to horzcat is a choice, not a must.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Bjorn Gustavsson
Bjorn Gustavsson 2020년 10월 8일
This gets you to a 2-by-suitable char array:
char(structure.field)
But this will obviously not be a general solution for fields of different data-types.
HTH

Ameer Hamza
Ameer Hamza 2020년 10월 8일
By default [] is equivalent to horizontal concatination hozcat(). Use vertcat() for vertical output
vertcat(structure.field)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by