0
votes

I would like to plot a straight line over a interval. For example, I have two variables: h and Time. When Time is between 0 to 0.56, the h value is 0.25. I need it to be a straight line. Similarly, for other points.When i use the function plot(Time,h), the lines are connected. I don't want this.

Need some guidance on this..

It looks like this: enter image description here

What I have tried so far?

function PlotH(Time,h)
    for i=2:size(Time)
        x = h(i)*ones(1,Time(i));
        hold on;
    end
    plot(x)
    ymax = max(h);
    xlim([1 max(Time)]);
    ylim([-0.5 ymax+0.5]);
    xlabel('Time')
    ylabel('Rate')
end
2
This should get you started: plot(Time,h,L). And set L=, based on what you want. L = '-' line, L = '.' points, L = '--' dashed line, L = 'o' empty points. Also, ever tried pressing F1 while hovering over a function in Matlab? quite helpfulThe-Duck
still not able to do it..lakesh
what is the code you used to plot the figure?The-Duck
that, as noted in my first comment will give you a connected line. If you want separate points you can use '.'. Try using plot(Time(1:5,h(1:5),'-');hold on;plot(Time(6:end),h(6:end),'.')The-Duck
i need straightline over a interval..lakesh

2 Answers

3
votes

Even simpler, just use stairs. This will take the value from the start of the interval, so to match the example and use the value from the end of each interval you'd need to shift h and Time relative to each other, e.g. stairs(Time(2:end), h(1:end-1)).

0
votes

Answer to question:

function PlotH(Time,h)
    for i=2:size(Time,1)
        plot([Time(i-1), Time(i)],[h(i), h(i)])
        hold on;
    end
    ymax = max(h);
    xlim([1 max(Time)]);
    ylim([-0.5 ymax+0.5]);
    xlabel('Time')
    ylabel('Rate')
end