How to use 'UniformOutput' and 'ErrorHandler' together in 'arrayfun' function?
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
Réponse acceptée
Stephen23
le 13 Déc 2022
Modifié(e) : Stephen23
le 13 Déc 2022
"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 commentaires
Stephen23
le 13 Déc 2022
Modifié(e) : Stephen23
le 13 Déc 2022
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Function Creation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!