Plot multiple arrays within a cell on a single figure

Hi folks,
I'm looking for a simplified method for plotting several matrices nested within a cell onto a single figure. Currently I'm using a for loop to cycle through the indices and plot each matrices individually:
figure(1)
for i=1:8
VP(i) = plot(cell{i}(:,1),cell{i}(:,2),'Marker','none',...
'LineWidth',3,'Color',C{i},'DisplayName',[scenario,'-',num2str(i)]);
hold on
end
The cell (cell) contains eight matrices. Here I'm plotting the second column of each matrix as a function of elements in their respective first column. I'd also like to have the colours be different -- hence the cell C{i} which takes a cell of prescribed colours and applies it to data set i in the loop. Variable VP allows me to easily generate a legend to delineate the different plots. Please let me know if there's a way to remove the need for a for loop. I was thinking about just replacing i with 1:8, but it doesn;t operate in the same manor as plotting a subset of a matrix:
VP(1:8) = plot(Array_cell{1:8}(:,1),Array_cell{1:8}(:,2),'Marker','none','LineWidth',3,'Color',C{1:8},'DisplayName',[scenario,'-',num2str(1:8)]);
Please let me know what you can come up with. I'd really appreciate the insight.

댓글 수: 4

Are all of your data sets the same size? Are the independant variables all the same values?
If you have eight different sets of data I'm pretty sure that you can't plot all of them at once, though others are free to correct me.
Also, I'm pretty sure you're going to need a loop to set the color for each of the lines anyway.
The arrays are unique in size. The independent variables are also unqiue.
Each array isn't too large, so the time to execute the loop is not astronomical. That being said, I'd like to remove an unnecessary for loop if a simple one line solution is available.
Thanks for the response. If I have to live with the loop, so be it.
If you're just looking to clean up your code you can replace the loop with a cellfun(), but the time will be similar because it's functionally the same as a for loop.
hold on
cellfun(@(x) plot(x(:,1),x(:,2)),Array_cell(1:8));
I'm not sure about line color off the top of my head, but you can probably set that property directly afterwards.
Stephen23
Stephen23 2019년 9월 19일
편집: Stephen23 2019년 9월 19일
You could use the plot syntax that accepts multiple X & Y pairs. Using a comma-separated list would make that simpler to work with.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2019년 9월 19일

편집:

2019년 9월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by