Calling Matlab from C++: How to pass in no arguments in varargin?

조회 수: 5 (최근 30일)
matal
matal 2013년 5월 30일
I am compiling Matlab .m functions using Matlab compiler into libraries callable from C++. The particular function I am working with looks like this:
function status = cainit( model, varargin )
if nargin==1
% do something
elseif nargin==2
% do something else
end
end
The C++ header file produced by Matlab compiler ("mcc") looks contains:
extern LIB_libca_CPP_API void MW_CALL_CONV cainit( int nargout, mwArray& status, const mwArray& model, const mwArray& varargin );
I want to call cainit from my C++ code such that the nargin==1 part of the Matlab function gets executed. But I can't figure out how to pass in "no arguments".
For example, if I try the most obvious:
mwArray status;
cainit( 1, status, mwArray(""), mwArray() );
It still results in nargin==2 because mwArray() is an empty ([]) array.
Is there a way not to pass in an argument from C++? In other words, how do I do the C++ equivalent of Matlab "cainit('mymodel')"?
Thanks in advance!

채택된 답변

matal
matal 2013년 5월 31일
Looks like this is not possible:
"Caution The C++ shared library interface does not support varargin with zero (0) input arguments. Calling your program using an empty mwArray results in the compiled library receiving an empty array with nargin = 1. The C shared library interface allows you to call mlfFOO(NULL) (the compiled MATLAB code interprets this as nargin=0). However, calling FOO((mwArray)NULL) with the C++ shared library interface causes the compiled MATLAB code to see an empty array as the first input and interprets nargin=1."

추가 답변 (1개)

Kaustubha Govind
Kaustubha Govind 2013년 5월 31일
I don't have a whole lot of experience with MATLAB Compiler, but does it work if you create varargin as an empty cell array and pass that in:
mwArray varargin(0, 0, mxCELL_CLASS);

카테고리

Help CenterFile Exchange에서 Deploy to C++ Applications Using mwArray API (C++03)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by