Vertical concatenation of structure fields (compact form)
9 次查看(过去 30 天)
显示 更早的评论
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'
>>
0 个评论
采纳的回答
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
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.
更多回答(2 个)
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
0 个评论
Ameer Hamza
2020-10-8
By default [] is equivalent to horizontal concatination hozcat(). Use vertcat() for vertical output
vertcat(structure.field)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!