# Bar3 crahes by plotting a cell array in a loop

1 回表示 (過去 30 日間)
CSCh 2023 年 5 月 2 日
コメント済み: CSCh 2023 年 5 月 9 日
Hi,
I have a M, which is a cell Array M, which is 1×1 cell array of {1×292 cell}. Each of the 292 consits again of cells different sizes
. M{1}=ans
1×292 cell array
Columns 1 through ...292
{1×288 cell} {1×288 cell} {1×287 cell} ...{1x260}.
Each of these cells consits of doubles with different number of rows but fixes amount of columns(15).
m{1}{1}= ans
1×288 cell array
Columns 1 through ...288
{34×15 double} {36×15 double} {37×15 double}.. {95x15}.
I would like to plot M with bar3. My code:
figure();
Az=(1:1:15);
for t1=1:292
for t2=1:length( M{1}{t1})
bar3(Az,(cell2mat(M{1}{t1}(t2))');
hold on;
end
end
After ~2h matlab has crahesd (killed)
Is there another way to plot it?

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

### 採用された回答

Nathan Hardenberg 2023 年 5 月 2 日
I think it is not quite clear what your result should be. One of your "doubles"-matrecies is already enough to plot a 3D-Bar plot. What you do is plotting multiple plots in the same figure. If this is your desired solution I don't think it is possible to remove the for-loop.
Still your (wanted solution) should result in a very messy figure. I made a simplified example in the form of your data below. You can see that the bars intersect each other and it is not really good to use as a visualization.
You can check the 'grouped' option in the documentation, which allows different bars at the same position. This would obviously need code adaptation. And on a datasize of yours the bars would probably be very hard to see.
As a tipp, you should create a small example with your data and work with this first, to avoid long loading times.
PS: The bracket "(" in front of cell2mat in your code is to much
N = {{[1,2,3,4,5; 6,7,8,9,10]},
{[1,2,3,4,5; 6,7,8,9,10]*2},
{[1,2,3,4,5; 6,7,8,9,10]*3},
{[1,2,3,4,5; 6,7,8,9,10]*4}};
M = {N};
figure(1); clf; hold on;
Az = (1:1:5);
for t1 = 1:length(M{1})
for t2 = 1:length( M{1}{t1} )
bar3(Az, cell2mat(M{1}{t1}(t2))');
end
end
view([-25.10 33.53])
##### 7 件のコメント5 件の古いコメントを表示5 件の古いコメントを非表示
CSCh 2023 年 5 月 4 日
Thank you very much Nathan for the code, now I just need a workaround for padarray.
Nathan Hardenberg 2023 年 5 月 4 日
A = [A; zeros(maxRows-rowA, colum)];
And remember to accept the answer if you are satisfied.

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

### その他の回答 (1 件)

CSCh 2023 年 5 月 5 日
Works great. Thank you.
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Nathan Hardenberg 2023 年 5 月 8 日
I would use the same loop and pad with NaNs:
B = [N{t1}{1}; nan(maxRows-rowA, colum)];
Then store all B matrecies in one 3 dimensional matrix
C = nan(maxRows, colum, 0) % define outside of loop
C(1:maxRows, 1:colum, end+1) = B; % inside of loop
Then just take the mean in the third axis and omit the NaN values.
mean(C,3,"omitnan")
This is not the optimal solution, but should be enough for one calculation.
It is really weird why the data is stored in the way it is when it is useful to take the mean of all values. Just wondering.
CSCh 2023 年 5 月 9 日
The data are stored daywise, each day has several amount of data. Thank you.

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

### カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by