use hold on to create a plot with many doubles
显示 更早的评论
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.
回答(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 个评论
Johnny Tong
2018-7-10
编辑:Johnny Tong
2018-7-10
Guillaume
2018-7-10
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);
类别
在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!