Reputation: 111
I want to draw a step graph using MATLAB.
In the X-axis, the value starts from 0
and increases like:
0
, 0+20=20
, 0+20+50=70
, 0+20+50+80=150
, 0+20+50+80+50=200
...
and corresponding Y-axis values are: Y = [0.76 1.10 1.28 1.35 1.35 1.45 1.50]
I mean, when:
X
value is 0-20
, Y
value is 0.76
X
value is 20-70
, Y
value is 1.10
X
value is 70-150
, Y
value is 1.28
,
X
value is 150-200
, Y
value is 1.35
,...
Please, help me to draw the step graph. My sample code is given below which showed an error due to the different size of X
and Y
.
X = [0 20 50 80 50 50 50 100];
Y = [0.76 1.10 1.28 1.35 1.35 1.45 1.50];
for i=1:length(X)
if i==1
X(i)=0
else
X(i) = X(i-1) + X(i)
end
end
figure
stairs(X, Y, 'LineWidth',2)
xlim([0 500])
grid
Upvotes: 3
Views: 850
Reputation: 19689
The end value of Y
needs to be repeated.
stairs(X, [Y Y(end)], 'LineWidth', 2);
Other than that, your loop is replaceable with the built-in function cumsum
.
X = cumsum(X);
Upvotes: 2