MATLAB: How to use a for loop to create new variables

forMATLABvariable assignment

I am very new to MATLAB so I apologize beforehand.
I have this code here which works fine:
t = 0:0.01:10;
y_01 = exp(-0.1*t);
y_1 = exp(-1*t);
y_3 = exp(-3*t);
plot(t,y_01,t,y_1,t,y_3)
However, I was wondering if I could condense it by using a for loop to have only one line for declaring the functions to be graphed, something like this:
t = 0:0.01:10;
for a = [0.1 1 3]
y.num2str(a) = exp(-a*t);
plot(a,y.num2str(a))
end
I did read a post that said trying to implement this is not a good idea (I think that's what the gist was) but I'm not 100% sure. I understand the "pseudo-code" above wouldn't plot them all on the same plot as well.

Best Answer

There are many posts that say trying to create numbered variables like that is discouraged, yes.
If you want to put all those plots on the same axes, you can do this without creating the individual variables.
t = 0:0.01:10;
% Tell MATLAB not to clear the axes each time you make a new plot
hold on
for M = [0.1, 1, 3]
plot(t, exp(-M*t));
end
% Optional: the next time you plot into this axes, the existing lines will be cleared
hold off