MATLAB: Plot different parts of vector

plot

Iยดve got a vector M(1x80) and would like to plot four different graphs (graph 1 equals values 1:20, graph 2 values 21:40, ...) and have a name of each of them in the legend (including a trendcurve):
I thougt of a loop like that:
for i = 1:20:80
plot(M3(i);
legend('Graph 1');
end
Can you tell me where the error is?
Thanks a lot for helping me!

Best Answer

Hi Robin,
The following code will provide what you are looking for
M3 = rand(1,80);
j = 1;
str = [];
for i = 1:20:80
hold on
plot(M3(i:19+i)); % Update this to have 20 values, which is fixed to 1 in your code
str = [str ["Graph " + num2str(j)]]; % To have different legend for each plot through string array
j = j+1;
end
legend(str)
Hope this helps.
Regards,
Sriram