16 views (last 30 days)

I'm having trouble learning how to use MEX files, and all of the documentation (even this one: https://www.mathworks.com/help/matlab/matlab_external/standalone-example.html#zmw57dd0e18781, which seemed simple at first but completely lost me after a couple paragraphs) is going WAY over my head.

Right now, I'm trying to use code from a .dll and I don't have the original source code, but I do have a header file with function prototypes of all of the functions that I need, all within extern "C".

So instead of changing these functions or really doing anything all that complicated, I'm just trying to use the .dll functions in my MATLAB code. Here's an example of one of my functions (I have twenty):

#include "mex.h"

#include "AllFunctions.h"

void mexFunction(int nlhs, mxArray *plhs[],

int nrhs, const mxArray *prhs[]){

if(nrhs != 0) {

mexErrMsgIdAndTxt("Error: myfun.c",

"Cannot have any inputs.");

}

if(nlhs != 1) {

mexErrMsgIdAndTxt("Error: myfun.c",

"One output required.");

}

double *output; //output

/* create the output matrix */

plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);

/* get a pointer to the real data in the output matrix */

output = mxGetPr(plhs[0]);

/* call the computational routine */

myfun;

}

It has no inputs and one output. Most of my functions have a few simple inputs and one output, and a few functions pass pointers. Loadlibrary completely fails for me, and the solutions given on multiple threads do not pertain to me, so I'm trying to use MEX's instead.

I'd like to know if this is the proper approach and maybe what I should include to make this actually work before I make twenty functions that don't work. They all work in tandem with each other, so it is very difficult to test one function and then move on from there. Thanks for helping me out.

PS: Please don't tell me I need to go study MEXs more, as I am doing so as you type up your response. If there is another thing I should be looking at, I'll be very grateful. Any help is appreciated, but please don't tell me to spend my time researching (unless its something specific) before I ask beginner questions because I did a lot of research before posting this question, and I am continuing to do so afterwards.

James Tursa
on 5 Jul 2017

Edited: James Tursa
on 5 Jul 2017

For the mex routine approach, assuming you can link with the library properly per Philip's comments, I have the following comments:

if(nlhs != 1)

mexErrMsgIdAndTxt("Error: myfun.cpp","One output required.");

The above line requires the user to specify an output variable when calling your code. This is usually too strict a requirement to place on a user. A more friendly approach is to require "at most 1" output" so that the result can simply go into ans. Note that even if nlhs==0, MATLAB always has room for at least one output in the plhs array. So this is advised:

if(nlhs > 1)

mexErrMsgIdAndTxt("Error: myfun.cpp","At most one output required.");

Then there are these lines:

/* create the output scalar */

out = mxGetScalar(plhs[0]);

/* call the computational routine */

out = myfun(in_one,in_two);

The above lines will crash MATLAB. The plhs[0] variable does not exist when you enter the mex routine ... that is something that you, the programmer, must create before you can use it. Thus the mxGetScalar(plhs[0]) call accesses invalid memory and crashes MATLAB. And even if you had created plhs[0] previously, the above lines of code would not get the next out result from myfun into plhs[0]. So these lines should be coded this way instead:

/* call the computational routine */

out = myfun(in_one,in_two);

/* create the output scalar */

plhs[0] = mxCreateDoubleScalar(out);

James Tursa
on 6 Jul 2017

Here is how I would do it:

double *pr;

:

/* create the output scalar */

plhs[0] = mxCreateDoubleMatrix(1,2,mxREAL);

/* call the computational routine */

out = myfun(in_one,in_two);

/* copy the output to plhs[0] */

pr = mxGetPr(plhs[0]);

pr[0] = out;

pr[1] = in_two;

Although I am confused by your comment "in_two changes by reference". There is nothing shown in your code that changes the in_two value. It will come back as exactly the same value that was passed into the mex routine (unless perhaps it is being passed by reference into myfun and modified there?).

James Tursa
on 7 Jul 2017

Philip Borghesani
on 5 Jul 2017

Edited: Philip Borghesani
on 5 Jul 2017

I suggest a two pronged approach.

- Get Loadlibrary working if at all possible. There is only one real error in your loadlibrary output (the last line) the rest are warnings caused by parsing windows.h and can safely be ignored. The error is the last line and is due to a missing #ifdef __cplusplus I think you fixed it in the header you are using for myfun for mex. Using loadlibrary will save you from writing a large number of simple mex files or a large mex file with a gateway.
- You may find that some functions are better called from a mex file. As alluded to by James Tursa, to call a dll from a mex file you must link with its corresponding library. The mex command gives information on how to link with an extra lib file. On Linux this is the same shared library that loadlibrary would use but on Windows this must be a .lib file. If you have mex configured for mingGW you may need to generate a lib file for that compiler. You were probably supplied with a lib file for Microsoft compilers.

I suggest new questions that are specific to current problems you are having with loadlibrary or mex. Solving multiple problems in a single answer causes confusion... Learning how a function is called from mex (or c) helps quite a bit in how to structure code that calls that function with loadlibrary

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_463416

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_463416

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_463466

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_463466

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_463700

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_463700

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_463702

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_463702

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_466752

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/345725-mex-programming-for-a-beginner#comment_466752

Sign in to comment.