syntax of this line tmp = cellfun(@(tbl) tbl(:, cellstr(dataVariables(ct))), trainDataNormalized, 'UniformOutput', false);% in RUL tutorial
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Stephen23
2024-11-27
编辑:Stephen23
2024-11-27
"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 个评论
更多回答(1 个)
Walter Roberson
2024-11-28
@(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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!