Use all table/timetable variables as input arguments of function without writing each variable manually.
Show older comments
I am doing some calculations with functions over tables values. The function uses all parameters from the defined table and it adds some results to the input table as new variables. This is because I have timetable values and want to calculate the function for all rows of the timetable. The following table contains the design properties from a heat exchanger.
B3_B4 = table(...
41,... % Plate number
10.53,... % Surface
6858,... % U_clean
0.1558,... % Distance A
0.0005,... % Plate thickness (0.5 mm)
0.0033,... % Plate distance (b)(3.3 mm)
0.0066,... % D_eq = 2×b
0.009,... % Pitch corrugation distance (9mm)
42.5 ... % Angle corrugacions
);
B3_B4.Properties.VariableNames([1 2 3 4 5 6 7 8 9]) = {'n_plac' 'Area' 'U_clean' 'dist_A' 'esp' 'dist_b' 'D_eq' 'S' 'beta'};
From this table I calculate the following function command in order to obtain some constant values for the given parameters.
[B3_B4.p1,B3_B4.p2,B3_B4.p3,B3_B4.p4,B3_B4.p5] = corrug_parameters(B3_B4.dist_b,B3_B4.S,B3_B4.beta);
I would like to use all the variables from the table as input arguments in the function. This means that instead of writting for each variable "B3_B4.Vars"
[B3_B4] = corrug_parameters(B3_B4)
I am also wondering if it possible to write a shorter command to store all the outputs values from a function to a table as new columns of it for each output. I am doing it manually as so the inputs, but when the functions get long it is really messy and I would like to know if it is possible to make it cleaner.
Note: I konw the example table is just one row and could be changed to an array or a vector, but I would like to keep it as table because of other similar calculations I do which are tables with more than one row.
Note 2: I have attached the table and the function.
Thanks.
Accepted Answer
More Answers (1)
Ameer Hamza
on 16 Mar 2020
Edited: Ameer Hamza
on 16 Mar 2020
In MATLAB, nothing is stopping you from using a table as input to a function. In MATLAB, variables of all classes can be used as input to a function (I am not aware of any exceptions). For example, change like this.
corrug_parameters.m:
function T_out = corrug_parameters(T_in)
gamma = (2 .* T_in.dist_b) ./ T_in.S;
p1 = exp(-0.15705 .* T_in.beta);
p2 = (pi .* T_in.beta .* gamma.^2) ./ 3;
p3 = exp(-pi .* (T_in.beta ./ 180) .* (1 ./ gamma.^2));
p4 = (0.061 + (0.69 + tan(T_in.beta .* (pi ./ 180))).^-2.63) .* (1 + (1 - gamma) .* 0.9 .* T_in.beta.^0.01);
p5 = 1 + T_in.beta ./ 10;
T_out = table(p1, p2, p3, p4, p5);
end
Call the functions like this
B3_B4 = table(...
41,... % Plate number
10.53,... % Surface
6858,... % U_clean
0.1558,... % Distance A
0.0005,... % Plate thickness (0.5 mm)
0.0033,... % Plate distance (b)(3.3 mm)
0.0066,... % D_eq = 2×b
0.009,... % Pitch corrugation distance (9mm)
42.5 ... % Angle corrugacions
);
B3_B4.Properties.VariableNames([1 2 3 4 5 6 7 8 9]) = {'n_plac' 'Area' 'U_clean' 'dist_A' 'esp' 'dist_b' 'D_eq' 'S' 'beta'};
out_table = corrug_parameters(B3_B4);
Categories
Find more on Tables 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!