How to plot points on a line

Say I have a random sequence of such as new=randn(5,1) which produces y values of
ans =
-0.9118
0.0494
1.0780
0.3082
0.2996
How would I plot these points as a line on a graph ie x=0, y=-0.9118 x=1 y=0.0494 etc or a line connecting the y values. I have tried
plot(new)
I would then like to add points to the graph as blue dots ie onto the graph of new=randn(5,1) add blue dots at the points from code such as
datapoints=
-0.9118
0.3082 or
datapoints=-0.9118 0.3082
I hope this make sense and thanks for the help.

 Accepted Answer

I suggest you to check hold on, and the LineSpec of the command plot
% your data
new = rand(5,1);
% open figure and retain current plot
figure, hold on
plot(new);
% new data points
datapoints = [-0.9118 0.3082]
% plot with blue circles
plot(datapoints,'bo')

6 Comments

jacob Mitch
jacob Mitch on 10 Oct 2019
Edited: jacob Mitch on 10 Oct 2019
Hiya im just having problems in that the datapoints are slightly shifted left of the minima points, I guess the y values match however the x value positions dont match. In that for ans y=-0.9118 x=1 match but y=0.3082 x=4 from ans doesnt match y=0.3082 x=2 from datapoints.
you must plot using x and y vectors. Can you share the code you used to calculate datapoints?
Hi Fabio my code is
function [xvalues, yvalues] = test(data)
c=1;
input=data;
x=length(input);
for z=2:x-x
elseif input(z-1)>input(z) && input(z)<input(z+1)
xvalues(c,1)=z;
yvalues(c,1)=input(z);
c=c+1
end
end
So I would like to graph the "data" points maybe as a lines connecting them then I would like to plot the minima of the graph that I get from "yvalues" ie if i had data as [3;0;3;1;3] and "yvalues" obtains the minima as [0;1], I would like the data drawn as lines connecting 3,0,3,1,3 for the y values and 3,0 as red circles at those points on the line. I would not like to use sign, find or diff functions. Thanks for your help again.
So it should be something like
% your data
new = rand(5,1);
% open figure and retain current plot
figure, hold on
plot(new);
% new data points
[xdatapoints,ydatapoints] = test(new);
% plot minima
plot(xdatapoints,ydatapoints,'ro'); % no line connecting minima
% plot(xdatapoints,ydatapoints,'ro-'); % line connecting minima
Note that it is not clear why the loop is for z=2:x-x. Why x-x?
Hi there, sorry it was just meant to be
for z=2:x
if z==x
return
This may be late but you're a genius. Thank you!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!