Plot multiple arrays within a cell on a single figure

20 views (last 30 days)
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 Comments
Bob Thompson
Bob Thompson on 19 Sep 2019
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 on 19 Sep 2019
Edited: Stephen23 on 19 Sep 2019
You could use the plot syntax that accepts multiple X & Y pairs. Using a comma-separated list would make that simpler to work with.

Sign in to comment.

Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!