Archive for January, 2011

01/21/2011

Thoughts – 1/21/2011

- 2011년 1월 21일 금요일 오후 1:56
5 학사 에스라가 모든 백성 위에 서서 저희 목전에 책을 펴니 책을 펼때에 모든 백성이 일어서니라 6 에스라가 광대하신 하나님 여호와를 송축하매 모든 백성이 손을 들고 아멘 아멘 응답하고 몸을 굽혀 얼굴을 땅에 대고 여호와께 경배하였느니라

나는 오늘 말씀을 어떻게 대하는가?

Sungkwang

01/17/2011

Print arguments in Mex function

#include “mex.h”

void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
int i;
char *str[500];

for (i=0; i
/* Check input to be sure it is of type char. */
if(!mxIsChar(prhs[i])){
mexErrMsgTxt(“Input must be of type char.”);
}
/* Copy the string data from prhs and place it into str. */
str[i] = mxArrayToString(prhs[i]);
}

/* Examine input (right-hand-side) arguments. */
mexPrintf(“\nThere are %d right-hand-side argument(s).”, nrhs);
for (i=0; i
mexPrintf(“\n\tInput Arg %i is of type:\t%s, %s”,i,mxGetClassName(prhs[i]),str[i]);
}

/* Examine output (left-hand-side) arguments. */
mexPrintf(“\n\nThere are %d left-hand-side argument(s).\n”, nlhs);
if (nlhs > nrhs)
mexErrMsgTxt(“Cannot specify more outputs than inputs.\n”);
for (i=0; i<nlhs; i++) {
plhs[i]=mxCreateDoubleMatrix(1,1,mxREAL);
*mxGetPr(plhs[i])=(double)mxGetNumberOfElements(prhs[i]);
}
}

This function print out the arguments of mex function.

>> mex mexTest.c
>> mexTest(‘-q’,'f’,'m’,'c’)

There are 4 right-hand-side argument(s).
Input Arg 0 is of type: char, -q
Input Arg 1 is of type: char, f
Input Arg 2 is of type: char, m
Input Arg 3 is of type: char, c

There are 0 left-hand-side argument(s).

01/17/2011

Passing Argument in Mex function without modifying C code

#include “mex.h”
#include “matrix.h”

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
{
if( !mxIsChar( prhs[i] ) ){
mexErrMsgTxt(“Input must be of type char.”);
return;
}
argv[i] = mxArrayToString( prhs[i] );
}

result = myfunction( argc, argv );

for( i=argc-1; i>=0; i– )
mxFree( argv[i] );
mxFree( argv );

if( result )
mexErrMsgTxt(“main function causes an error”);
}

This function pass the arguments string to the main function.If you have a large piece of C code, it might be cumbersome process to change code to fit mex style. But this code will just read arguments list and pass it to the main function.

For example, if the c function call is

myfunciton -f1 mpeg4.flt -mc mc_filename

then, the mex function call is

myfunction(‘myfunction’,'-f1′,’mpeg4.flt’,'-mc’,'mc_filename’)

Note that first argument for mex function is function name because c function keeps first argument (argv[0]) funciton name.

<How to use>

1. Put this code in front of the C code.

2. Change the name ‘main’ to different name like ‘myfunction.’

3. Compile mex file by typing ‘mex myfunction.c’ in Matlab command window.

 

 

 

Follow

Get every new post delivered to your Inbox.