MATLAB Answers

Iterate through several tables of different sizes to perform calculations separately

3 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
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

  3 Comments

Peter Perkins
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
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!