HelloMex.c

#include "mex.h" void mexFunction ( int nlhs, mxArray *plhs[ ], int nrhs, const mxArray *prhs[ ] ) { plhs[ 0 ] = mxCreateString("Hello World!"); return; }

Explaining HelloMex.c

All this function does is to create a string (using mxCreateString) and assign it to the output pointer, so that it's sent back to MATLAB's workspace.

plhs --> Pointer to Left-Hand Side ==> pointer to the output

 

Here's how to compile and run HelloMex.c

» ls HelloMex*
helloMex.c

» mex HelloMex.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 11.00.7022 for 80x86
Copyright (C) Microsoft Corp 1984-1997. All rights reserved.

» HelloMex
ans =
Hello World!

» ls HelloMex*
HelloMex.c HelloMex.dll

»

 

We could have also used mexPrintf routine in the HelloMex.c if we didn't want the string to be returned to MATLAB. The program would have looked like:
#include "mex.h" void mexFunction ( int nlhs, mxArray *plhs[ ], int nrhs, const mxArray *prhs[ ] ) { mexPrintf("Hello World!\n"); return; }


Back to Table of Contents