Naming tables inside a for loop

13 views (last 30 days)
Ahmad
Ahmad on 10 Oct 2022
Edited: Jan on 10 Oct 2022
I want to change names of tables inside a for loop. Each iteration inside the for loop represent one table.
N=2;
for ii = 1:N
if ii = 1
jj = 'Attendance'
else if ii = 2
jj = 'Absence'
end
Table_'Attendance' = table(variables);
Table_'Absence' = table(variables)
end
end
I want to generate a seperate table at each iteration. I generate same variables at each iteration with different data. That's why I want to put them in tables with different names.
For example, here I mentioned that I want to have two tables and I want to name them "Table_Attendance" and "Table_Absence", respectively. Is there a way to do this?

Accepted Answer

Jan
Jan on 10 Oct 2022
Edited: Jan on 10 Oct 2022
This is the most frequently asked question in this forum. The answer is easy: Don't do this. It is a shot in your knee.
Better:
NameList = {'Attendance', 'Absence'};
for ii = 1:2
TableList.(NameList{ii}) = table(variables);
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!