Is it possible to pass no arguments in order to invoke default values for functions compiled into a C++ shared library with the MATLAB Compiler?

12 views (last 30 days)
I would like to create a C++ shared library using the MATLAB Compiler that I can call from my C++ program. I would like to be able to use default values in my shared library function when no argument is supplied. In MATLAB, I can use the NARGIN function to detect when arguments are not specified.
For example:
function c = increment_matrix(a,b)
% Returns c = a + b.
% If b is not specified, then c = a + 1.
% If a and b are not specified, then c = 0.
if nargin < 2, b = 1; end
if nargin < 1, a = -1; end
c = a + b;
When I compile this into a C++ shared library with the MATLAB Compiler, it creates a function signature requiring two input arguments, but I would still like to be able to use the function with one or no input arguments.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Feb 2010
Because the MATLAB Compiler creates a function signature that matches the one in the MATLAB file, you cannot call a library function with fewer or more arguments than is defined in the function.
You can work around this as follows:
1. Add ISEMPTY conditions to your MATLAB file, so that you can also pass empty variables into your function. These empty variables will be replaced by your default values.
For example:
function c = increment_matrix(a,b)
% Returns c = a + b.
% If b is not specified, then c = a + 1.
% If a and b are not specified, then c = 0.
% if nargin < 2, b = 1; end % OLD
% if nargin < 1, a = -1; end % OLD
if nargin < 2 || isempty(b), b = 1; end % NEW
if nargin < 1 || isempty(a), a = -1; end % NEW
c = a + b;
2. In your C++ program, create an empty mwArray to use for passing to your functions.
For example:
mwArray empty_input_argument(0, 0, mxDOUBLE_CLASS, mxREAL);
3. In your C++ program use the empty input argument (from Step 2) to emulate a function call with fewer arguments.
For example:
increment_matrix(1,out1,in1,in2); // Both input arguments specified
increment_matrix(1,out1,in1,empty_input_argument); // One input argument specified.
increment_matrix(1,out1,empty_input_argument,empty_input_argument); // No input arguments specified.
The following files are attached to help illustrate this.
- increment_matrix.m
The source code for the increment_matrix function used in this solution.
- libincrmtrx.dll
The C++ shared library that was generated with the MATLAB Compiler by issuing the following command:
mcc -W cpplib:libincrmtrx -T link:lib increment_matrix.m -v
- libincrmtrx.ctf
The CTF archive file that was also generated from the same command issued to create libincrmtrx.dll above.
- call_increment_matrix.cpp
The source code to a sample C++ program that calls the increment_matrix function from the libincrmtrx C++ shared library.
- call_increment_matrix.exe
The compiled sample C++ program, which requires libincrmtrx.dll and libincrmtrx.ctf to be in the same directory during execution. The program should produce the following output:
The value of 'increment_matrix(1,2)' is:
3
The value of 'increment_matrix(1)' is:
2
The value of 'increment_matrix()' is:
0

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!