Use all table/timetable variables as input arguments of function without writing each variable manually.

19 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

dpb
dpb el 16 de Mzo. de 2020
Editada: dpb el 16 de Mzo. de 2020
As a start altho need more to know precisely what else to do, look at\
>> rowfun(@corrug_parameters,B3_B4,'InputVariables',{'dist_b','S','beta'},'NumOutputs',5,'OutputVariableNames',cellstr(num2str([1:5].','P%d')))
ans =
1×5 table
P1 P2 P3 P4 P5
_________ ______ _______ _______ ____
0.0012625 23.934 0.25175 0.43535 5.25
>>
You can simply append this table output to B3_B4 if the intent is to add to it...
B3_B4=[B3_B4 rowfun(@corrug_parameters,B3_B4,'InputVariables',{'dist_b','S','beta'}, ...
'NumOutputs',5, ...
'OutputVariableNames',cellstr(num2str([1:5].','P%d')))];
The above will work only once, of course; once the table has been augmented by those variables then can't (and wouldn't want to) add same ones again. Then you would, presumably, just recalculate.
You can also access row/column variables in a table by numeric indices in either direction and/or by cellstrings which can also be program variables. See the background information on accessing data from a table for all the possible addresssing modes; they're myriad.
Alternatively to pass the full table to your function it would have to be written to use the needed parameters from the table. That limits its functionality to be the table only unless you want to allow either and build in code to check for whether a table or the independent variables are passed. Possible, just depends on how much troouble want to go to and how see using it going forward as to whether worth the effort.
ADDENDUM:
One way to minimize the typing is to not use multiple variable names with sequential subscripts like P1, P2, .... Instead, use an array or maybe a struct.
  2 comentarios
Roger Gomila
Roger Gomila el 17 de Mzo. de 2020
I am using your code and it just worked fine! But when I got to a function which needed input arguments from the B3_B4 table and another table, I don't know how to write the input argumetns. I tried this and didn't work:
daily_B3_bruto=[daily_B3_bruto rowfun(@calculs,daily_B3_bruto,'InputVariables',...
{'PRI_IN','PRI_OUT','SEC_IN','SEC_OUT','rho','cP','mu','k','CAUDAL_EQUIP'}, ...
B3_B4,... % Second input table
'InputVariables',{'Area','U_clean','esp','D_eq','p1','p2','p3','p4','p5'},...
'NumOutputs',7, ...
'OutputVariableNames',{'cab_massic','LMTD','U','U_cabal','Q','R_total','R_fou'})];
Is it possible to work with more than one input table?
dpb
dpb el 17 de Mzo. de 2020
Directly w/ rowfun, no, it has only a single table input argument. You could, however, write a wrapper function that did the upfront work to build a temporary table to pass to the other function to hide the details.

Iniciar sesión para comentar.

Más respuestas (1)

Ameer Hamza
Ameer Hamza el 16 de Mzo. de 2020
Editada: Ameer Hamza el 16 de Mzo. de 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);

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by