Creating subplots based on a cell array

Hi,
If I have two cells like this:
cell1 = {'AA' 'AA' 'BC' 'BC' 'BC' 'DD' 'DD'; 1 2 3 4 5 6 7; 7 6 5 4 3 2 1; 5 9 8 7 6 5 4}
cell1 = 4×7 cell array
{'AA'} {'AA'} {'BC'} {'BC'} {'BC'} {'DD'} {'DD'} {[ 1]} {[ 2]} {[ 3]} {[ 4]} {[ 5]} {[ 6]} {[ 7]} {[ 7]} {[ 6]} {[ 5]} {[ 4]} {[ 3]} {[ 2]} {[ 1]} {[ 5]} {[ 9]} {[ 8]} {[ 7]} {[ 6]} {[ 5]} {[ 4]}
cell2 = {'AA' 'AA' 'BC' 'BC' 'BC' 'DD' 'DD'; 3 4 5 6 7 8 9; 2 3 4 5 6 7 8; 9 8 7 6 5 4 3}
cell2 = 4×7 cell array
{'AA'} {'AA'} {'BC'} {'BC'} {'BC'} {'DD'} {'DD'} {[ 3]} {[ 4]} {[ 5]} {[ 6]} {[ 7]} {[ 8]} {[ 9]} {[ 2]} {[ 3]} {[ 4]} {[ 5]} {[ 6]} {[ 7]} {[ 8]} {[ 9]} {[ 8]} {[ 7]} {[ 6]} {[ 5]} {[ 4]} {[ 3]}
Is there a way to create subplots based on matching the strings from the first row, where there is not necessarily an equal number of strings for all?
The final figures would look something like this:
Thank you!

4 件のコメント

the cyclist
the cyclist 2023 年 7 月 11 日
Is the pattern of alphabetic characters (e.g. 'AA') guaranteed to be identical between the two cell arrays?
Stephen23
Stephen23 2023 年 7 月 11 日
編集済み: Stephen23 2023 年 7 月 11 日
Storing lots of scalar numeric inside cell arrays is very inefficient data design, and complicates the code required to process it. Why are you not using two arrays (i.e. a text vector and a numeric matrix) or a simple table instead?
Liam
Liam 2023 年 7 月 11 日
Yes, they will be sorted relative to each other already in a prior step
Liam
Liam 2023 年 7 月 11 日
Currently, the "titles" and data are actually stored how you said - text vector and a numeric matrix. I just assumed appending them together into a cell array would maybe make this subplot sorting easier.

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

 採用された回答

Voss
Voss 2023 年 7 月 11 日
編集済み: Voss 2023 年 7 月 11 日

1 投票

cell1 = {'AA' 'AA' 'BC' 'BC' 'BC' 'DD' 'DD'; 1 2 3 4 5 6 7; 7 6 5 4 3 2 1; 5 9 8 7 6 5 4};
cell2 = {'AA' 'AA' 'BC' 'BC' 'BC' 'DD' 'DD'; 3 4 5 6 7 8 9; 2 3 4 5 6 7 8; 9 8 7 6 5 4 3};
assert(isequal(cell1(1,:),cell2(1,:)))
figure('Position',[50 50 500 1200]); % make a tall figure to see the subplots clearly
[g,g_id] = findgroups(cell1(1,:));
N = numel(g_id);
for ii = 1:N
idx = find(g == ii);
n = numel(idx);
for jj = 1:n
subplot(N*n,1,(ii-1)*n+jj)
plot([cell1{2:end,idx(jj)}],[cell2{2:end,idx(jj)}]);
title(cell1{1,idx(jj)});
end
end

1 件のコメント

Liam
Liam 2023 年 7 月 13 日
Modifying this a little worked perfectly for me, thanks!

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

その他の回答 (1 件)

the cyclist
the cyclist 2023 年 7 月 11 日

1 投票

cell1 = {'AA' 'AA' 'BC' 'BC' 'BC' 'DD' 'DD'; 1 2 3 4 5 6 7; 7 6 5 4 3 2 1; 5 9 8 7 6 5 4};
cell2 = {'AA' 'AA' 'BC' 'BC' 'BC' 'DD' 'DD'; 3 4 5 6 7 8 9; 2 3 4 5 6 7 8; 9 8 7 6 5 4 3};
numbers1 = cell2mat(cell1(2:end,:));
numbers2 = cell2mat(cell2(2:end,:));
[uniqueAlpha,~,indexFromUniqueToAll] = unique(cell1(1,:));
numberUniqueAlpha = numel(uniqueAlpha);
for na = 1:numberUniqueAlpha
columnsThisAlpha = find(indexFromUniqueToAll==na);
numberColumnsThisAlpha = numel(columnsThisAlpha);
figure
tt = tiledlayout(numberColumnsThisAlpha,1);
title(tt,uniqueAlpha(na))
for nc = 1:numberColumnsThisAlpha
nexttile
plot(numbers1(:,nc),numbers2(:,nc))
end
end

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2023 年 7 月 11 日

コメント済み:

2023 年 7 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by