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

29 views (last 30 days)
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.

Accepted Answer

Daniel M
Daniel M on 9 Nov 2019
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.

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!