Iterate through several tables of different sizes to perform calculations separately

22 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

Accepted Answer

Stephen23
Stephen23 on 14 Nov 2020
Edited: Stephen23 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
  5 Comments
Peter Perkins
Peter Perkins on 8 Nov 2021
Only saw this a year later. By "meaningful", I did not mean "they are data". If you want to do "calculations" on them, then yes, and quite often the variable you should use is a categorical, not plain text. But by "meaningful", I meant "useful to access that subset of your full set of data", which is what the OP was doing. Compare what youi'd need to do to get at the "A" chunk of this:
>> C = {"A" rand(3); "B" rand(4); "C" rand(5)}
C =
3×2 cell array
{["A"]} {3×3 double}
{["B"]} {4×4 double}
{["C"]} {5×5 double}
>> C{vertcat(C{:,1})=="A",2}
ans =
0.19182 0.81973 0.60101
0.98324 0.13645 0.17688
0.24405 0.39814 0.82835
vs. the "A" chunk of this:
>> s.A = C{1,2}; s.B = C{2,2}; s.C = C{3,2}
s =
struct with fields:
A: [3×3 double]
B: [4×4 double]
C: [5×5 double]
>> s.A
ans =
0.19182 0.81973 0.60101
0.98324 0.13645 0.17688
0.24405 0.39814 0.82835
That's why structs have field names and tables have variable names. Metadata, not data.
Vlatko Milic
Vlatko Milic on 6 Jan 2022
Hi,
I came across this comment and answer and I find the conversation very interesting. I have a similar problem but I have problems saving the table names, and also generating the tables post-processing in the function (i.e. saving them in the main script).
In detail, I want to perform mathematical operations within each table, basically by multiplying the values from two columns and creating a new column in each table . Therefore, I am thinking about creating a new function with "Table 1, Table 2 and Table 3" as input . Moreover, the output I want is basically new versions of "Table 1, Table 2 and Table 3" which include a new column. However, the problem for me lies in creating a new function with the old "Table 1, Table 2 and Table 3" as input. I tried this segment of code (does not work unfortunately).
V = {Table_1,Table_2,Table_3}
%%
for k = 1:numel(V)
[R_W_L(V,:)] = my_calc(V{k})
end
I get the error "A table row subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string array, or a cell array of character vectors." When I look at the columns of the tables I see that they are doubles, e.g. 1000*1 double. In addition, I have difficulties knowing how I can connect the table names (i.e. Table 1,Table 2 etc). to the function "my_calc"

Sign in to comment.

More Answers (1)

Natalie Salazar
Natalie Salazar on 30 Sep 2021
Expanding on this question. How would I save the outputs as seperated tables?

Categories

Find more on Structures 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!