How to create one plot with multiple lines in a for loop?

Hello,
I have been trying to create a plot, with an unknown number of lines. Each should be on the same graph, and a different colour. However, I am only getting one line on my graph.
This is my code currently:
y = cell(numFiles,1) ;
x = [30:10:100]
figure();
hold on
for k = 1:numFiles
%Code that generates data
y{k} = [h g f e d c b a ]
plot(x,y{k}, 'Color', [rand,rand,rand])
end
title('SNR / Index')
hold off
What am I doing wrong? Thank you!

 采纳的回答

x = 30:10:100;
figure();
% axes('NextPlot', 'add'); % Same as: hold on
hold on
for k = 1:8
y{k} = rand(size(x));
plot(x, y{k}, 'Color', rand(1, 3))
end
You see, it works with hold('on') or with creating the axes explicitly also.
Are you sure, that this is your code and there is no cla anywhere?

5 个评论

It still isn’t working using hold on or axes().
All the code is mine. I just don’t really understand why only one line shows, as from my limited knowledge, it should.
But you see, that my code, which only replaces your "y{k} = [h g f e d c b a ]" by some random data, does show a set of lines. So the difference must be inside, what is hidden in "y{k} = [h g f e d c b a ]". Show us this important part.
As assume, your code contains a command, which clear the axes: imshow(i) command in generating_prob() .
You can find such problems by using the debugger: Set a breakpoint in the code and step through the functions line by line. Then you will see, which command has the observed effekt.
Thank you! This worked! I need to use the debugger more often.
The debugger is the best friend of the programmer.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

提问:

AK
2021-5-27

评论:

Jan
2021-5-28

Community Treasure Hunt

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

Start Hunting!

Translated by