MATLAB: Creating multiple inputs depending on conditions

input

Is there a way to create multiple inputs depending on initial condition? To be clear, for an example I want to create n inputs depending of numeric value of symbol n.
n=input('Input n');
and I enter 5 and I want to create 5 inputs automatically in real time, outputs should look like this:
a1=input('Input1')
a2=input('Input2')
a3=input('Input3')
a4=input('Input4')
a5=input('Input5')
Is that possible? Is there any way to create it?

Best Answer

Yes, there is a way, but it is a bad idea to do so. Dynamically naming variables with trailing numbers like that makes it very difficult to use them downstream in a generic way. You would be better off using a cell array or the like. E.g.,
for k=1:n
a{k} = input(['Input' num2str(k)])
end
Now you can use natural indexing with a{k} downstream in your code instead of hard coding a1, a2, etc or resorting to eval( ). Or if the inputs are scalars simply ask the user to enter an n-element vector, and then you can use a(k) downstream in your code.