syntax of this line tmp = cellfun(@(tbl) tbl(:, cellstr(dataVariables(ct))), trainDataNormalized, 'UniformOutput', false);% in RUL tutorial
30 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Fabio Ricci
el 27 de Nov. de 2024 a las 21:44
Respondida: Walter Roberson
el 28 de Nov. de 2024 a las 6:23
I'm following this tutorial
in order to understand it and be able to doing something similar independently.
but I can't understanwhat does this line do
tmp = cellfun(@(tbl) tbl(:, cellstr(dataVariables(ct))), trainDataNormalized, 'UniformOutput', false);
I know that
-cellfun applies a function on every cell of a cell array
-trainDataNormalized is a cell array that was created in the script
-'UniformOutput', false specifies a setting for cellfun
but, I can't figure out what @(tbl) tbl is supposed to do , I didn't create neither a function or a variable with such a name, the script is working as expected though
Can anyone explain ?
0 comentarios
Respuesta aceptada
Stephen23
el 27 de Nov. de 2024 a las 22:02
Editada: Stephen23
el 27 de Nov. de 2024 a las 22:30
"I can't figure out what @(tbl) tbl is supposed to do , I didn't create neither a function or a variable with such a name, the script is working as expected though"
Yes, you did. Right there in that very code you defined a variable named tbl which is the input to an anonymous function**: https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
tmp = cellfun(@(tbl) tbl(:, cellstr(dataVariables(ct))), trainDataNormalized, 'UniformOutput', false);
% ^^^ you defined a variable named TBL right here.
That anonymous function then uses some other variables dataVariables(ct) (that already exist in the workspace) to get some values out of tbl (assuming that tbl is a table). Basically for every table in the cell array you access the same columns/variables.
Perhaps it would be clearer if you allocated that anonymous function to a function handle on another line:
fnh = @(tbl) tbl(:, cellstr(dataVariables(ct)));
tmp = cellfun(fnh, trainDataNormalized, 'UniformOutput', false);
Or you could replace it with a loop if you wish, something like this:
for k = 1:numel(trainDataNormalized)
tbl = trainDataNormalized{k};
tmp{k} = tbl(:, cellstr(dataVariables(ct)));
end
** Note that the function itself has no name. That is what anonymous means.
0 comentarios
Más respuestas (1)
Walter Roberson
el 28 de Nov. de 2024 a las 6:23
@(tbl) tbl(:, cellstr(dataVariables(ct)))
That syntax outputs the handle of an anonymous function.
The anonymous function accepts a single parameter. For the duration of the body of the anonymous function, the parameter is referred to as tbl . During execution of the code, all occurances of tbl in the body of the code will be replaced by the content of the first parameter that is passed to the anonymous function.
The code is functionally the same as
@(varargin) varargin{1}(:, cellstr(dataVariables(ct)))
except that the varargin version does not check the number of inputs to the anonymous function, but the @(tbl) version checks to be sure the anonymous function is invoked with at most one input.
0 comentarios
Ver también
Categorías
Más información sobre Data Type Identification 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!