MATLAB: How to draw piecewise functions

not enough argumentspiecewise function

hello, i need to draw the given piecewise function then calculate g(t+1), 3*g(t),etc but my code is unable to draw the function. it is not showing any graph, when i delete first function and write the lines separately it says it had to be inside of a function, if i put the first function at the bottom it gives the error of "not enough arguments".
function execute(~)
t= -4:0.01:10;
plot(t,g(t));
end
function y = g(t)
if t<-2
y=0;
elseif -2<t & t<0
y=-4-2*t;
elseif 0<t & t<4
y=-4+3*t;
elseif 4<t & t<8
y=16-2*t;
else
y=0;
end
end

Best Answer

This will get you started:
g = @(t) [(-4-2*t).*((t > -2) & (t < 0)) + (-4+3*t).*((t > 0) & (t < 4)) + (16-2*t).*((t > 4) & (t < 8))];
t = linspace(-10, 10);
figure(1)
plot(t, g(t))
grid
I leave the rest (and figuring out how it works) to you.