Why my error handling function not working for multiple outputs?

1 回表示 (過去 30 日間)
Struggling in MATLAB
Struggling in MATLAB 2022 年 12 月 23 日
編集済み: Stephen23 2022 年 12 月 23 日
I am using arrayfun with multiple outputs as following.
[entryTime,exitTime] = arrayfun(@(id) entryExitTimeStampFun(id), ...
input_data.id(startRow:endRow),'ErrorHandler', ...
@entryExitTimeStampErrorHandler);
I have written a entryExitTimeStampErrorHandler.m function in the same folder as entryExitTimeStampFun.m as follows
function [entryTime,exitTime] = entryExitTimeStampErrorHandler
entryTime = nan;
exitTime = nan;
end
entryExitTimeStampFun.m fetches data from a database corresponding to the input id. But it fails for the cases where the data is now deleted in the database corresponding to that id.
It worked fine for me with missing id's for a single output using this following code.
logicalOutput = arrayfun(@(id) passingCentralZone(id,0.5), ...
input_data.id(startRow:endRow),'UniformOutput', false, 'ErrorHandler',@(varargin) false);
So I am afraid something is wrong with my errorhandling function.

採用された回答

Stephen23
Stephen23 2022 年 12 月 23 日
編集済み: Stephen23 2022 年 12 月 23 日
"Why my error handling function not working for multiple outputs?"
The problem has nothing to do with the two outputs, the problem is the inputs... namely that your function does not have any, even though the ARRAYFUN() documentation clearly states that "The first input argument of the error handler is a structure ... The remaining input arguments to the error handler are the input arguments for the call to func that made func throw the error." So it states very clearly, that inputs will be supplied to the function when it is called. The error handler example in the ARRAYFUN() documentation also show VARARGIN being used to collect all of those input arguments:
I already explained this and showed fully working examples in a comment to your earlier question:
For some reason you decided to ignore the documentation (and my examples) and remove the input arguments.
Result: errors.
Solution: accept the input arguments that the documentation specifies:
function [entryTime,exitTime] = entryExitTimeStampErrorHandler(varargin)
% ^^^^^^^^ You need this.
% why did you remove it?
PS: note that (also as explained in my previous answer) your error handler function could be reduced to this:
erf = @(varargin) deal(NaN);
  1 件のコメント
Struggling in MATLAB
Struggling in MATLAB 2022 年 12 月 23 日
Yes, I just figured it out. And it solved the problem. I was trying with input 'id' in place of 'varargin' earlier, but it gave me a yellow flag. So I removed all inputs. Thanks again for help with the same problem. Now I am using this following with the errorhandling function as you mentioned.
[entryTime,exitTime] = arrayfun(@(id) entryExitTimeStampFun(id), ...
input_data.id(startRow:endRow),'UniformOutput',false,'ErrorHandler', ...
@entryExitTimeStampErrorHandler);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by