How to use 'UniformOutput' and 'ErrorHandler' together in 'arrayfun' function?
16 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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?
Risposta accettata
Stephen23
il 13 Dic 2022
Modificato: Stephen23
il 13 Dic 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 Commenti
Stephen23
il 13 Dic 2022
Modificato: Stephen23
il 13 Dic 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Function Creation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!