use hold on to create a plot with many doubles
Show older comments
i have many cells each have some doubles, i would like to use hold on to plot out all the cells in one plot. beside using
hold on
plot(x,y1)
plot(x,y2)
....
plot(x,y92)
hold off
is there other methods which can provide a much faster and flexible way to do it? because i have a lot of data like this. very thanks first.
Answers (1)
The main idea is to avoid hiding indices in the names of variables. See Why and how to avoid EVAL. Create an array instead:
y = rand(15, 10);
x = 1:10;
AxesH1 = subplot(1,2,1, 'NextPlot', 'add'); % NextPlot=add is same as "hold on"
for k = 1:15
plot(AxesH1, x, y(k, :);
end
or even smarter:
AxesH2 = subplot(1,2,2);
plot(AxesH2, x, y)
This draws 15 lines simultaneously.
Note: Do not use indices hidden in the name of variables. Using indices as indices is much more direct, clear and efficient.
2 Comments
Johnny Tong
on 10 Jul 2018
Edited: Johnny Tong
on 10 Jul 2018
Guillaume
on 10 Jul 2018
There are no indication that anything is a cell array in your question. Certainly, y1, y2, ... y92 can't be cell arrays as plot does not accept cell arrays as inputs.
However, since your yn are all different size, they should indeed be stored in a cell array. So you should have a single y variable which is {y1, y2, ..., y92}. You need to fix whatever created these numbered variables to generate the cell array instead. See the link in Jan's answer.
Once you have a y cell array, it is indeed trivial to do all the plots:
hold on
for idx = 1:numel(y)
plot(x, y{idx});
end
Or with cellfun:
hold on
cellfun(@(v) plot(x, v), y);
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!