plot data from cell array cellfun()
Show older comments
Hi,
I concatentated cell arrays using: C1 = {};
to get a cell array that is 1x15 with the following:
In cells 1 to 3 there are 8x1 cells
In cells 2 to 6 there are 2x1 cells
In cells 7 to 9 there are 6x1, 2x1, 2x1 cells
In cells 10 to 12 there are 2x1, 1x1, 2x1 cells
In cells 13 to 15 there are 2x1, 1x1, 2x1 cells
I'm trying to plot each row of the cell array within the overall cell array C1 using this:
figure
hold on
cellfun(@(C1) plot(C1(1,:)),'o-',C1)
hold on
cellfun(@(C1) plot(C1(2,:)),'o-',C1)
All the way to row 8 as there are 8 rows in C1.
Does anyone have suggestions as I've tried cell2mat but since the columns have different lengths I can't horizontal concatentate the different lengths so I used the cell arrays?
I want to plot each row with a different color.
Thanks
4 Comments
Walter Roberson
on 31 Mar 2020
cellfun(@(C1) plot(C1(1,:)),'o-',C1)
That is inconsistent with your description.
You said that C1 is a 1 x 15 cell array. You said that in cell 1 there is an 8 x 1 cell. That implies that C1{1} is an 8 x 1 cell array.
When you cellfun C1, each entry of the cell array is opened and the contents passed to the function. The first would be opening C1{1} which is an 8 x 1 cell array. You would be passing that 8 x 1 cell array to plot(C1(1,:)) under the confusing name C1, not the same as the other C1. With C1 being an 8 x 1 cell array, C1(1,:) would be a 1 x 1 cell array. But you cannot plot() a cell array.
If you had said
cellfun(@(C1) plot(C1{1,:}),'o-',C1)
then you would be asking to plot the _contents of C1{1}{1} and that is potentially possible, if C1{1}{1} is a numeric array.
Mohammad Sami
on 31 Mar 2020
Edited: Mohammad Sami
on 31 Mar 2020
I am not sure what you mean plot all rows, since each nested cell only has one value per row.
if you just want to concatentate everything you can do as follows.
c2 = vertcat(C{:});
c2 = cell2mat(c2);
plot(c2);
James
on 31 Mar 2020
Answers (1)
Walter Roberson
on 31 Mar 2020
hold on;
cellfun(@(C1) plot(cell2mat(C1), 'o-'), C);
hold off
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!