MATLAB: I want to produce square wave without using square function, but this code it showing error, “illegal use of IF keyword”. How to rectify it

if statementsquare wave

t=-10:0.1:10;
a=1;
for i=1:2:201
{
if a==1
{
z(1,i)=0;
z(1,i+1)=0;
}
else
{
z(1,i)=1;
z(1,i+1)=1;
}
end
a=a*(-1);
}
end
plot(t,z)

Best Answer

In MATLAB, {} is used only for building cell arrays, or extraction of data from collection objects, such as cell arrays or tables or string scalars. You should remove all of those { and } that you have.
Related Question