Q:

Splitting a plot with if..else statement in matlab

I want to simply plot a function with two different styles before and after a specific value. For example, for -2*pi<= x <0 with line width of 1 and for 0<= x <=2*pi with line width 2. I have written 
for x=-2*pi:0.1:2*pi
   y = sin(x);
   if x < 0
     plot(x,y,'LineWidth',1)
     grid on
     hold
   else
     plot(x,y,'LineWidth',2)
     grid on
     hold
   end
end

But it doesn't work

A:

There's no need to loop, just define vectors of boolean values to define the regions:
x = -2*pi:0.1:2*pi;
y = sin(x);
idx_period1 = (x >= -2*pi) & (x < 0);
idx_period2 = ~[idx_period1(2:end),false]; % ensure the regions touch.
plot(x(idx_period1),y(idx_period1),'LineWidth',1);
hold on
plot(x(idx_period2),y(idx_period2),'LineWidth',2);

If you want points rather than a connected line then use,
idx_period1 = (x >= -2*pi) & (x < 0);
idx_period2 = ~idx_period1;
scatter(x(idx_period1),y(idx_period1),'Marker','.','MarkerSize',1);
hold on
scatter(x(idx_period2),y(idx_period2),'Marker','.','Markersize',2);

