How to use 'UniformOutput' and 'ErrorHandler' together in 'arrayfun' function?
16 次查看(过去 30 天)
显示 更早的评论
I am using 'arrayfun' function to run a function of an array which gives output logical true/false or nan depending upon trajectory of animal. Here 0.5 is the length on the central zone in X and Y direction since it is a 2-dimensional zone.
[id, output] = arrayfun(@(id) passingCentralZoneRejectInitialPresence(id,0.5), ...
input_data.id(startRow:endRow),'UniformOutput', false, 'ErrorHandler',@(s,varargin) false());
I understand since my output could be logical or double I have to use 'UniformOutput', false. But I don't understand how to use error handler in this case. The one I am using here is an answer to my previous post. (Thanks for that) I wanted logical false as output for the trials with bad data.
Can anyone please check if my code is correct and explain what is 'ErrorHandler' doing in this case or direct me to a more elaborate documenation?
采纳的回答
Stephen23
2022-12-13
编辑:Stephen23
2022-12-13
"Can anyone please check if my code is correct..."
It is not: you have called ARRAYFUN() with two output arguments, therefore both the function applied to the array and the ErrorHandler will need to also return two output arguments. Your ErrorHandler function only returns one output argument.
"...and explain what is 'ErrorHandler' doing in this case or direct me to a more elaborate documenation?"
The ErrorHandler function is very simple: if the main function applied to the array throws an error for any specific input array elements (so does not provide any output values), then the ErrorHandler is called to provide those output elements. Clearly if you request two output arguments from ARRAYFUN(), then both functions must provide two output arguments.
You can see the example in the documentation also has two output arguments. It defines a function (in a file), not an anonymous function, but you can use DEAL() to return mutlple outputs from an anonymous function:
fnh = @(id) passingCentralZoneRejectInitialPresence(id,0.5);
erf = @(varargin) deal(false);
[id, output] = arrayfun(fnh, input_data.id(startRow:endRow), ...
'UniformOutput',false, 'ErrorHandler',erf);
4 个评论
Stephen23
2022-12-13
编辑:Stephen23
2022-12-13
PPS: lets try that error handler and see if it works:
arr = {1,2,uint8(3),4}; % fake data: three doubles and one integer type.
fnh = @(id) deal(sqrt(id),true); % SQRT() fails on integer data types.
erf = @(~,id,varargin) deal(id,false);
[val,boo] = cellfun(fnh, arr, 'UniformOutput',false, 'ErrorHandler',erf)
val{:}
So far everything is working exactly as expected and documented.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!