convert a nested struct in a matrix

Hi, I have a nested struct
userTouristicTraj(1).touristicData(:).traj --> [1;1][1;1;1;1;1][1;1;1;1][1;1;1;1;1;1];[1;1];
userTouristicTraj(2).touristicData(:).traj --> [4;4][5;5]
userTouristicTraj(3).touristicData(:).traj --> [12;12;12;12;6][12;12]
and I want to trasform it in a matrix
A= [1 1 0 0 0 0; 1 1 1 1 1 0; 1 1 1 1 0 0; 1 1 1 1 1 1; 1 1 0 0 0 0; 4 4 0 0 0 0; 5 5 0 0 0 0; 12 12 12 12 6 0; 12 12 0 0 0 0]
I have try to use the functions struc2cell and cell2mat but the code doesn't run and maybe I have to use a different way
a=struct2cell(userTouristicTraj);
b=cell2mat(a);
Error using cell2mat (line 64)
The field names of each cell array element must be consistent and in consistent order.
Can you help me, please? thanks

 採用された回答

Guillaume
Guillaume 2017 年 6 月 5 日
編集済み: Guillaume 2017 年 6 月 5 日

0 投票

Assuming that all your structures have the same fields in all the substructures:
alltdata = [userTouristicTraj.touristicData]; %concatenate all touristicData into a single structure
alltraj = {alltdata.traj}; %and extract all traj into a cell array
maxlength = max(cellfun(@numel, alltraj));
alltraj = cellfun(@(v) [v.', zeros(1, maxlength - numel(v))], alltraj, 'UniformOutput', false) %pad as necessary
alltraj = vertcat(alltraj{:});

4 件のコメント

elisa ewin
elisa ewin 2017 年 6 月 5 日
the code gives an error
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in @(v)[v,zeros(maxlength-numel(v),1)]
Guillaume
Guillaume 2017 年 6 月 5 日
Sorry, I missed that your vectors were column vectors (which you want transposed as row vectors).
I've fixed the code.
elisa ewin
elisa ewin 2017 年 6 月 5 日
thanks for your help; I have tried the new code but it gives the same error
Guillaume
Guillaume 2017 年 6 月 5 日
Hopefully, fixed for good this time.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2017 年 6 月 5 日

編集済み:

2017 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by