How to avoid error when whether the function has returns is unknown?

4 ビュー (過去 30 日間)
埃博拉酱
埃博拉酱 2019 年 10 月 29 日
コメント済み: 埃博拉酱 2019 年 10 月 30 日
function Output = PairArrayArrayfun(Function,Array1,Array2,ArrayDimension,UniformOutput)
CatDimension=max(ndims(Array1),ndims(Array2))+1;
Output=cellfun(@(Cell)Function(Cell{1},Cell{2}),PackIntoCell(cat(CatDimension,SplitIntoCell(Array1,ArrayDimension),SplitIntoCell(Array2,ArrayDimension)),CatDimension),"UniformOutput",false);
if UniformOutput
Output=cell2mat(Output);
end
end
My function accept a parameter "Function", which is expected to be a function pointer offered by the caller. However, I don't know whether this caller-offered function has any returns. In my current code, if the given function returns nothing, an error will occur.
I want my function to return nothing if the caller's function returns nothing; to return something if the caller's function returns something. How should I code?
Try-catch is a bad idea since the caller's function will be called for more than expected times. This may cause more issues if the given function has side effects.
The given function in the usecase is very likely to be anonymous, so nargout(Function) will always return -1.
The built-in function cellfun should face the same issue, and MATLAB developers successfully solved it. However, its source code is unavailable.

採用された回答

埃博拉酱
埃博拉酱 2019 年 10 月 30 日
This is my workaround:
function Output = PairArrayArrayfun(Function,Array1,Array2,ArrayDimension,UniformOutput)
CatDimension=max(ndims(Array1),ndims(Array2))+1;
if nargout>0
Output=cellfun(@(Cell)Function(Cell{1},Cell{2}),PackIntoCell(cat(CatDimension,SplitIntoCell(Array1,ArrayDimension),SplitIntoCell(Array2,ArrayDimension)),CatDimension),"UniformOutput",false);
if UniformOutput
Output=cell2mat(varargout);
end
else
cellfun(@(Cell)Function(Cell{1},Cell{2}),PackIntoCell(cat(CatDimension,SplitIntoCell(Array1,ArrayDimension),SplitIntoCell(Array2,ArrayDimension)),CatDimension),"UniformOutput",false);
end
end
In this way the responsibility to judge output is kicked back to the caller. It seems clumsy but may be how the built-in cellfun works.
  4 件のコメント
Daniel M
Daniel M 2019 年 10 月 30 日
Right, but you shouldn't have to leave it up to the user. You can do both things. You can call nargout(Function) to find out how many outputs there should be, and reflect that in how your function behaves. You can also check nargout() too.
埃博拉酱
埃博拉酱 2019 年 10 月 30 日
@Daniel M
nargout(Function) is unreliable. If it returns -1, that means Function returns varargout. A varargout return could be nothing! abs(nargout(Function))>0 doesn't assure the Function of any returns.

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

その他の回答 (2 件)

Daniel M
Daniel M 2019 年 10 月 29 日
編集済み: Daniel M 2019 年 10 月 29 日
You can use nargout(Function) to check the number of output arguments. <https://www.mathworks.com/help/matlab/ref/nargout.html>
  4 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 30 日
In my opinion it is a bug in the function design to define a varargout function that returns no output if requested by the user. That said, I have seen it in a couple of Mathworks functions.
埃博拉酱
埃博拉酱 2019 年 10 月 30 日
編集済み: 埃博拉酱 2019 年 10 月 30 日
@Walter Roberson Anonymous functions are always supposed to return varargout by nargout, even for those ones that really return nothing. In this usecase the given function is very likely to be anonymous, unfortunately.

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


Walter Roberson
Walter Roberson 2019 年 10 月 30 日
try/catch the first execution outside of the cellfun. If the assignment works then cellfun the rest of the iterations. If it fails then you will need to loop instead of cellfun.
cellfun cannot be used with functions that produce no output.
  1 件のコメント
埃博拉酱
埃博拉酱 2019 年 10 月 30 日
You're wrong. cellfun really can work with no-return functions, at least for R2019b

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

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by