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?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Piyush Gupta
el 5 de Ag. de 2020
Respondida: Steven Lord
el 5 de Ag. de 2020
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)
0 comentarios
Respuesta aceptada
Walter Roberson
el 5 de Ag. de 2020
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.
0 comentarios
Más respuestas (1)
Steven Lord
el 5 de Ag. de 2020
In some languages (like C) you need to use {} around sections of code (like the bodies of for loops or if statements.) MATLAB is not one of those languages.
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)
I'll leave it to you to decide what to do about the extra element in z that prevents you from calling plot on t and z.
Based on this mistake (trying to use another language's syntax in MATLAB) I'm guessing you're new to MATLAB. If that's the case, I recommend going through the free MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to familiarize yourself with the basics of MATLAB syntax.
0 comentarios
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!