Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional input arguments on Matlab function

Tags:

matlab

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?

like image 423
Marta Sampietro Avatar asked Sep 25 '26 22:09

Marta Sampietro


1 Answers

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'
like image 79
Daniel Avatar answered Sep 27 '26 16:09

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!