# Iterate through several tables of different sizes to perform calculations separately

3 views (last 30 days)
Alex Rich on 13 Nov 2020
Commented: Stephen Cobeldick on 19 Nov 2020
I have 3 tables of different sizes, T1, T2, T3. In the body of my code I have several functions perform calculations on these tables, one table at a time (by brute force for now), and then save the results. Is it possible to loop through them or do I need to create a pointer to each table? Is a pointer to a table possible in matlab? I can't pass them on to the for loop as I've tried below due to their differing size (and they will always be different)... any help is much welcomed.
T1 = %stuff 1
T2 = %stuff 2
T3 = %stuff 3
for tables_to_be_manipulated = [T1 T2 T3]
mycalcualtionfunction(tables_to_be_manipulated);
mysavefunction(tables_to_be_manipulated);
end

Stephen Cobeldick on 14 Nov 2020
Edited: Stephen Cobeldick on 14 Nov 2020
"Is it possible to loop through them or do I need to create a pointer to each table? Is a pointer to a table possible in matlab?"
The MATLAB approach is to put them into one cell array (which they should be anyway, and is essentially just a list of pointers to your tables) and then use basic indexing:
c = {T1,T2,T3};
for k = 1:numel(C)
mycalcualtionfunction(C{k});
mysavefunction(C{k});
end

Alex Rich on 14 Nov 2020
This worked great. Thank you for your help, much appreciated!
Peter Perkins on 19 Nov 2020
It would also be possible to put the three tables in a scalar struct whose fields are named T1, T2, and T3, and use structfun to iterate. If the names are meaningful, this preserves them. If "three" is really just a stand-in for "three hundred", then a cell array is probably what you want.
Stephen Cobeldick on 19 Nov 2020
"If the names are meaningful" then they should be stored in an array as data, e.g. in a field of a structure or variable of a table or in the cells of a cell array. Accessing this data will be much simpler more efficient than messing around with structure fieldnames or table variable names. For example it would then be trivial to sort and compare this data.