MATLAB: How to plot several graphs with slightly different name in a loop

arraymultiple plots

I want to plot many graphs in a loop. But every plot should get a description like h1, h2 and so on, that I can change the settings after plotting all the data. How does this work? I have a read a lot that I need an array, but what do I have to change at the fourth line (names(i))?
x = {'2';'3';'4'};
names = [strcat('h',x)]
for i=[2,3,4]
names(i)=plot(xx,yy);
end
set(h2,'Linewidth',1.5);

Best Answer

You were looking for eval. Stephen Cobeldick is very pasionate about this topic (and with good reason), so I have taken the liberty of adapting some of his thoughts on this topic: Introspective programming ( eval and other functions like it) is slow, and the MATLAB documentation warns specifically against it. Using eval is slow, makes debugging difficult, and removes all code helper tools (code checking, syntax hints, code completion, variable highlighting, etc.). A longer discussion can be found here. If you are not an expert and you think you need eval, there are probably better solutions to your problem.
The code below should solve your problem.
x = [2,3,4];
names = sprintfc('h%d',x);
h=zeros(size(names));
for i=[2,3,4]
yy=rand(1,10);xx=1:10;
h(i)=plot(xx,yy);hold on
end
set(h(2),'Linewidth',1.5);
legend(h(x),names)