Is there a way to use different sets of input arguments for one function?
조회 수: 16 (최근 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.
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!