That figure looks like a sine function, so let's just assume that it is for this example. While I don't have MATLAB in front of me right now, what I would probably do is, in an m-file script:
clear all; clc;
functionToPlot = [sin(0 : (pi/2) : (8*pi))]; %This spacing will look very sharp and pointy, so I'd recommend using >>linspace like shown in other answers.
yAxisVector = [-1 : 1 : 1];
for n = 1 : length(functionToPlot)
if rem(functionToPlot(1,n),2) <= pi
plot(functionToPlot(1,n),yAxisVector,'r')
hold on
elseif rem(functionToPlot(1,n),4) <= pi
plot(functionToPlot(1,n),yAxisVector,'g')
hold on
elseif rem(functionToPlot(1,n),6) <= pi
plot(functionToPlot(1,n),yAxisVector,'y')
hold on
elseif rem(functionToPlot(1,n),8) <= pi
plot(functionToPlot(1,n),yAxisVector,'c')
hold on
end
end
This code should give you the function that you pictured in your question. Have you tested a code yet? What code did you test? This link shows an alternative method using RGB values, if you prefer that. Good luck with your project!