Is there a way to use different sets of input arguments for one function?

29 ビュー (過去 30 日間)
Theo Seidel
Theo Seidel 2019 年 11 月 9 日
回答済み: Daniel M 2019 年 11 月 9 日
I have ct data that I want to process but the function either has to read the data out of a .raw-file before it can process it or it can use it immediately in case it has already been extracted from the file prior to that. So it would be convenient to have a function that either takes the file name or the ct data together with a variable that has information about the spatial dimensions of the ct data:
function processCT(filePath,varargin)
...
end
function processCT(ct,ctDimensions,varargin)
...
end
Usually I'd use the inputParser class for optional arguments but I'm not sure if this is possible: If filePath is set it would still be possible to give the other two arguments (but it shouldn't be) and if ct is set it would still wouldn't be an error to not include ctDimension (but it should).
In default implementations it seems to be possible: many functions (e.g. the interp2 function) have different possible sets of required arguments (e.g. either V,k or X,Y,V,Xq,Yq and also optional arguments)
I'd just like to know how to do that if it's possible.

採用された回答

Daniel M
Daniel M 2019 年 11 月 9 日
It just requires you to check the type of each input argument. I think it would be easier to use a struct (or even an object) with fieldnames (properties) of filepath, data, and dimensions. Then you just pass in the object and check which properties exist or which are empty and handle it like that.

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by