Optional graphics handle as first argument in function

Hi,
similar to this question, I am creating some plotting functions and would like to stay consistent with Matlab built-in behavior. I.e. have a graphics handle (axes, figure, TiledChartLayout etc.) as an optional first argument for my functions (e.g. plot, nexttile).
I can see how to achieve it using varargin and an InputParser but am wondering if this can be done using the function argument validation scheme (which I appreciate a lot).

 採用された回答

Stephen23
Stephen23 2022 年 4 月 25 日
編集済み: Stephen23 2022 年 4 月 25 日

0 投票

From my reading of the "Function Argument Validation" documentation here:
there is currently no way to have optional leading arguments like that.
Here is a possible untested workaround, by defining a very simple wrapper function:
function myfun(one,varargin)
if ishghandle(one)
locfun(one,varargin{:})
else
locfun(gcf,one,varargin{:})
end
end
function locfun(obj,A,B,C,...)
arguments
..
end
end

1 件のコメント

chris
chris 2022 年 4 月 25 日
Thanks for the suggestion. It anyway is a bit uncommon, I would say, to have a leading optional argument. So no wonder this is hard to reproduce.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 4 月 25 日

コメント済み:

2022 年 4 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by