简体   繁体   中英

Optional input arguments on Matlab function

I need to implement a function that does some image processing on a specific number of images (nFrames) that are located in a folder (folder1). The function would look something like:

function imgProc( nFrames,path )

Let's say I have several folders with different numbers of images in each one. What I need to have is optional input arguments, meaning that if the user wants, he can do the image processing for the first 10 images, for example, but if he does not specify the number, the function should perform the task on all the images. And the same for the folders, there should be a default folder in case the user does not specify from which folder he wants to take the images. It also could be interesting that the user could call the function with 0, 1 or 2 input arguments.

I have thought of using the exist function like this:

function imgProc( nFrames,path )

if exist( path,'var' ) == 0
    path = 'img/record_space';
end

if exist( nFrames,'var' ) == 0
    d = dir([ path,'\*.png' ]);
    nFrames = length( d( not([ d.isdir ]) ) );
end

end

But if I call the function with no input arguments it gives an error saying there's not enough input arguments. Is it possible to create a function that can have all of its arguments optional and moreover allow you to input 0, 1 or 2 according to your needs, taking into account that one is a number and the other one a string?

To fix the problem in your code:

function imgProc( nFrames,path )

if exist( 'path','var' ) == 0
    path = 'img/record_space';
end

if exist( 'nFrames','var' ) == 0
    d = dir([ path,'\*.png' ]);
    nFrames = length( d( not([ d.isdir ]) ) );
end

end

exists expects the variable name, not the variable itself. You can pass a variable containing a string, but then it would check if that string exists:

x='y'
exist(x,'var') % checks if y exists
exist('x','var') %checks if x exists

What I recommend to have a flexible interface is using the inputParser

function imgProc( varargin )
p = inputParser;
addOptional(p,'frames',inf,@isnumeric);
addOptional(p,'path',pwd,@(x)exist(x,'dir'));
parse(p,varargin{:});
%lower frames if required to the maximum possible value
frames=min(p.Results.frames,numel(dir(fullfile(p.Results.path,'*.png'))));
%if frame was previously a number (not inf) and is lowered, print a warning.
if frames<p.Results.frames&&p.Results.frames~=inf
    warning('parameter frames exceeded number of images present. Frames set to %d',frames);
end
disp(p.Results);
end

Possible ways to call the function:

>> imgProc
    frames: Inf
      path: 'D:\Documents\MATLAB'

>> imgProc('frames',1)
    frames: 1
      path: 'D:\Documents\MATLAB'

>> imgProc('path','foo')
    frames: Inf
      path: 'foo'

>> imgProc('path','bar','frames',9)
    frames: 9
      path: 'bar'

In newer versions on Matlab, you can also call the function using this syntax, eg:

imgProc()
imgProc( frames=1 )
imgProc( path="foo" )
imgProc( path="bar", frames=9 )

The result will be the same as Daniel's answer above, but it makes the function calling more intuitive (in my opinion).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM