MATLAB: Saving a new table for every loop iteration

anti-patternantipatternbuggydynamic variable namesevalfor loopfunctionindexinginefficientslow

I am having trouble saving a function output in a for loop with a different variable name
variable_max = size(variable_scan,1);
for i = 1:variable_max
data_name = variable_scan{i,1};
column_name = variable_scan{i,2};
name = strcat('data_table',num2str(i));
name = dataextract(data,data_name,column_name);
dataextract outputs a table. I would like to save this output with something like table_'column_name'

Best Answer

Don't do that. Dynamic variable creation is a bad idea and totally unnecessary.
Use a cell array.
data = cell(variable_max,1);
column_name = cell(variable_max,1);
%and preallocate other variables, this is important if you want your code to be faster
for k=1:variable_max
column_name {k,1} = ...
data{k,1} = dataextract(...)
Now you have all your tables under one variable. You can access them by indexing.
Related Question