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

15 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 String Parsing 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by