cell arrays matlab,How does one concatenate cell arrays that are part of a cell array in MATLAB?

There are a few more details about your cell contents that would be needed for a more general solution, but if the names property always returns a cell array of strings, and if your structures are all scalars (i.e. 1-by-1 structure arrays), then the following solution using CELLFUN, CHAR, and CELLSTR will give you an N-by-1 cell array of strings allNames containing all of your names:

allNames = cellfun(@(x) {char(x.names)},allData);

allNames = cellstr(char(allNames{:}));

And here's an example where allData contains three different structures:

>> allData{1} = struct('names',{{'hello'}},'junk',1:3);

>> allData{2} = struct('names',{{'hi' 'yo' 'hey' 'whassup'}});

>> allData{3} = struct('names',{{'howdy';'aloha'}},'junk',4);

>> allNames = cellfun(@(x) {char(x.names)},allData);

>> allNames = cellstr(char(allNames{:}))

allNames =

'hello'

'hi'

'yo'

'hey'

'whassup'

'howdy'

'aloha'

EDIT:

Generalizing to the case where the names property returns a cell array of objects, not necessarily strings, you can try this solution which reshapes each cell array into an M-by-1 cell array, then vertically concatenates all of them into an N-by-1 cell array of objects:

allNames = cellfun(@(x) {reshape(x.names,[],1)},allData);

allNames = vertcat(allNames{:});

Or, if you would rather end up with a 1-by-N cell array of objects, you can do this:

allNames = cellfun(@(x) {reshape(x.names,1,[])},allData);

allNames = [allNames{:}];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值