How do I concatenate a single field from a multi-dimensional struct?

I have a 1xN struct where a specific field is always an Mx1 cell array of strings, and I wish to concatenate all of these cell arrays without using a for loop. Any idea of how I may do this? Here's an example to illustrate what I mean:
var_info =
1×8 struct array with fields:
name
vars
path
size(var_info(1).vars)
ans =
51 1
size(var_info(6).vars)
ans =
8 1
I can achieve this now using for loops (see below) But I figured there must be some more elegant method of doing so that doesn't use as much memory. (Note: The unique function is included so I can get rid of duplicate strings as I go.)
entire_list = {};
for i = 1:length(var_info)
entire_list = [entire_list; var_info(i).vars];
entire_list = unique(entire_list);
end
Thanks.

 采纳的回答

Simply:
{var_info.vars}

4 个评论

Thanks for the quick answer. However, that code gives me a cell array of cell arrays, but I would like to have it as all one array.
{var_info.vars}
gives me :
1×8 cell array
Columns 1 through 6
{51×1 cell} {51×1 cell} {51×1 cell} {51×1 cell} {11×1 cell} {8×1 cell}
Columns 7 through 8
{8×1 cell} {6×1 cell}
But I would like the output to be:
{237x1 cell}
I just figured out the answer. using the function cat on the first dimension works perfectly.
cat(1,var_info.vars)
ans =
237×1 cell array
@jack_mscinc, as per Stephen's comment,
cat(1, var_info.vars)
is the same as
[var_info.vars];
%or
horzcat(var_info.vars);

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by