필터 지우기
필터 지우기

Plot multiple arrays within a cell on a single figure

조회 수: 20 (최근 30일)
Lucas Warwaruk
Lucas Warwaruk 2019년 9월 19일
편집: Stephen23 2019년 9월 19일
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
Bob Thompson
Bob Thompson 2019년 9월 19일
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개)

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by