3

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?

2 Answers 2

5

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'
Sign up to request clarification or add additional context in comments.

4 Comments

That works just fine, but I have one minor question. How would I put the default frames value to be the exact number of images that can be found in the input/default path?
@MartaSampietro: That is not supported and I think what you ask for is wrong. You want the default to be all files, not a certain number of files. Let a be your default directory with 5 files and b another directory with 10 files. You probably want to process 10 frames in this case.
I think I didn't explain so well. For example, let's say you call the function like in the third case in your answer. The user gives the folder path, but not the number of frames he wants to process, so the nFrames variable should adopt the value of the total number of images inside the folder the user inputs.
@MartaSampietro: I have updated my code how I would implement it.
0

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

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.