MATLAB: How to create a large number of variables with names that differ from each other by indices (X_1, X_2…)

assigncreatedynamicgenerateMATLABnamestringvariables

I want to create 100 variables with names such as X_1,X_2, …,X_100 then assign values to these variables.

Best Answer

The following example shows how to create multiple variables X_1, …X_5 with assigning values to them.
var = 'X_';
vari = cell(50,1);
for i = 1:50
vari(i) = {[var num2str(i)]};
end
for i = 1:50 %assign values to the variables for example X_i = 25*i
assignin('base', vari{i}, 25*i);
end