MATLAB Answers

0

Plot multiple arrays within a cell on a single figure

Asked by Lucas Warwaruk on 19 Sep 2019
Latest activity Edited by Stephen Cobeldick on 19 Sep 2019
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

Show 1 older comment
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.
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.

Products


Release

R2019a

0 Answers