lakshmen
lakshmen

Reputation: 29094

Plot straight line over a interval in MATLAB

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

Upvotes: 0

Views: 1508

Answers (2)

Notlikethat
Notlikethat

Reputation: 20984

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)).

Upvotes: 3

lakshmen
lakshmen

Reputation: 29094

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

Upvotes: 0

Related Questions