How do you plot elements of a cell array?

Given a cell array where each element contains a vector of numbers of type double, I select a few elements to analyze. They are all collected for the same x data. In general, how would I generate a figure with plots of any arbitrary number of cell array elements of vectors of type double?
Would it make sense to output the desired cell array elements into a matrix and then plot that? The corresponding matrix of x values would (I think) be the same size, and the columns would be identical to one another since the x data are the same. If that's the case, how do I do that?

 채택된 답변

Cameron
Cameron 2023년 1월 13일
Loop through your cells and use the hold on then hold off command.
cellData{1} = [(1:20)',(1:20)' + rand(20,1)];
cellData{2} = [(1:20)',2*(1:20)' + rand(20,1)];
cellData{3} = [(1:20)',3*(1:20)' + rand(20,1)];
cellData{4} = [(1:20)',4*(1:20)' + rand(20,1)];
hold on
for CellColumn = 1:length(cellData)
plot(cellData{CellColumn}(:,1),cellData{CellColumn}(:,2),'-o')
end
hold off

댓글 수: 4

L'O.G.
L'O.G. 2023년 1월 13일
But how do I do this for arbitrary number of cell arrays? Here you selected 4, but what if that input can change? I'd like it to be a bit more robust.
@L'O.G. he just created 4 because you forgot to read the Community Guidelines and therefor forgot to attach your data, so he had to make up some. He just chose 4 arbitrarily. However if you look at the loop:
for CellColumn = 1:length(cellData)
plot(cellData{CellColumn}(:,1),cellData{CellColumn}(:,2),'-o')
end
you'll see that it is general and will work for any number of cells in the cell array.
See the FAQ:
Any way to do this by avoiding for loops? I have a large cell array and I find that the plot(x,y); hold on really slows down the plotting.
Cameron
Cameron 2023년 5월 8일
Can you give an example of what your data looks like?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2023년 1월 13일

댓글:

2023년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by