Error when calling the mexFunction

1 回表示 (過去 30 日間)
shdotcom shdotcom
shdotcom shdotcom 2019 年 9 月 20 日
コメント済み: shdotcom shdotcom 2019 年 9 月 21 日
I have build the mexFunction for this source code. However, I have got this error when I call the main function
argc 3
argv --tfError using callFun
main function causes an error
#include <mex.h>
#include <matrix.h>
#include<stdio.h>
#include <math.h>
#include <limits.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "ants.h"
#include "utilities.h"
#include "InOut.h"
#include "TSP.h"
#include "timer.h"
#include "ls.h"
// ========================================================================
/* --- main program ------------------------------------------------------ */
// ========================================================================
int callFun(int argc, char *argv[]){
printf ( "\nargc %d",argc);
printf ( "\nargv %s",argv[1]);
long int i;
start_timers();
}
// ========================================================================
/* --- mex function----------------------------------------------------- */
// ========================================================================
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int argc = 0;
char **argv;
int i, result;
argc = nrhs;
argv = (char **) mxCalloc( argc, sizeof(char *) );
for (i = 0; i < nrhs; i++){
if( !mxIsChar( prhs[i] ) ){
mexErrMsgTxt("Input must be of type char.");
return;
}
argv[i] = mxArrayToString( prhs[i] );
}
result = callFun( argc, argv );
for( i=argc-1; i<=0; i-- )
mxFree( argv[i] );
mxFree( argv );
if( result )
mexErrMsgTxt("main function causes an error");
}
Call the function:
callFun('callFun','--tf', 'tsp',
the problem is in this line:
printf ( "\nargv %s",argv[1]);
, but, I could not understand why?

採用された回答

James Tursa
James Tursa 2019 年 9 月 20 日
You probably should have gotten a warning from the compiler that your callFun( ) function doesn't return anything, even though the signature says it does. When you get funny behavior like this, it is best to use the -v option in your mex command and look at all of the compiler and linker messages since they might tell you exactly what is wrong. You should add something like this to the bottom of the callFun( ) function:
return 0; // <-- or whatever value is appropriate
The way it is coded now, your mexFunction is picking off a garbage value from a register and treating it as the function return value.
Also, this line
for( i=argc-1; i<=0; i-- )
should be this instead
for( i=argc-1; i>=0; i-- ) // changed <= to >=
  1 件のコメント
shdotcom shdotcom
shdotcom shdotcom 2019 年 9 月 21 日
Thank you very much for your help. Is there anything that I can do to improve the mexFunction. I have used the code in this post. However, This code is too old.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by