How to use 'UniformOutput' and 'ErrorHandler' together in 'arrayfun' function?

3 ビュー (過去 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), ...,'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,, ...
'UniformOutput',false, 'ErrorHandler',erf);
  4 件のコメント
Struggling in MATLAB
Struggling in MATLAB 2022 年 12 月 13 日
I appreciate you taking your time and effort to explain this. It makes sense to me now. Thank you very much!


その他の回答 (0 件)


Find more on Loops and Conditional Statements in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by