Questions tagged [mex]

MEX-files (MEX stands for Matlab EXecutable) are dynamically linked subroutines produced from C, C++ or Fortran source code that, when compiled, can be run from within MATLAB® in the same way as MATLAB functions or built-in functions.

In general, MATLAB can be quite efficient in performing a wide variety of numerical tasks. However, from time to time there are certain tasks that require more "low-level" control over the execution. For these kind of tasks MATLAB allows for an easy interface to C/C++/Fortran subroutine pre-compiled and ready to be used as any built-in function.

Here are some useful links for MEX-files:

1483 questions
8
votes
2 answers

How to add "help"-text to a mex-function?

I am writing a Matlab mex-file. However, mex-files seem to have a serious limitation: help mexfilename won't cause a help text to appear. I could circumvent this by writing a m-file, that ultimately calls the mex-file, but includes help, but there…
bastibe
  • 16,551
  • 28
  • 95
  • 126
8
votes
1 answer

How can I get MatLab to find the Visual C++ Compiler?

Preamble: I found a solution in the midst of writing this, and this problem was a PITA and had a convoluted solution. Thus, I feel compelled leave this here to help any poor soul who has this problem. I'm new to MatLab, and I don't usually use…
drmuelr
  • 955
  • 1
  • 13
  • 30
8
votes
2 answers

Makefile: Converting C-code to mex code (Linking error)

I have a C-code which works fine using makefile. Now, I am trying to convert it to mex file so that I can run it from Matlab. Here also, I am using makefile approach. But, makefile for mex gives me error. Here is the tsnnls lib that I want to…
Garima Singh
  • 1,410
  • 5
  • 22
  • 46
8
votes
2 answers

Version GLIBCXX_3.4.11 not found (required by buildW.mexglx)

I am trying to compile a c++ ubuntu project via matlab here. When I am trying to use it after the compilation with make command, I am getting the following error: Invalid MEX-file '////fashionista_v0.2/lib/+bsr/buildW.mexglx': …
Jose Ramon
  • 5,572
  • 25
  • 76
  • 152
8
votes
4 answers

Correctly over-loading a stringbuf to replace cout in a MATLAB mex file

MathWorks currently doesn't allow you to use cout from a mex file when the MATLAB desktop is open because they have redirected stdout. Their current workaround is providing a function, mexPrintf, that they request you use instead. After googling…
user27315
  • 538
  • 5
  • 8
8
votes
1 answer

MATLAB's tic-toc & C's clock discrepancy

I have written some C code which I call form MATLAB after I compile it using MEX. Inside the C code, I measure the time of a part of the computation using the following code: clock_t begin, end; double time_elapsed; begin = clock(); /* do stuff...…
Pantelis Sopasakis
  • 1,902
  • 5
  • 26
  • 45
8
votes
1 answer

MATLAB parfor and C++ class mex wrappers (copy constructor required?)

I'm trying to wrap a C++ class in a matlab mex wrapper using the approach outlined here. Basically, I have an initialization mex file which returns a C++ object handle: handle = myclass_init() I can then pass this to another mex file (e.g.…
Ken Chatfield
  • 3,277
  • 3
  • 22
  • 27
8
votes
3 answers

Matlab: Does calling the same mex function repeatedly from a loop incur too much overhead?

I have some Matlab code which needs to be speeded up. Through profiling, I've identified a particular function as the culprit in slowing down the execution. This function is called hundreds of thousands of times within a loop. My first thought was…
Sundar R
  • 13,776
  • 6
  • 49
  • 76
7
votes
1 answer

c++/MATLAB Mex binding

I'm about to write a Mex File, so I can use my c++ code in MATLAB. This code is built around a singleton class (in c++). I've read that for memory to be persistent I have to use MxAlloc isntead of malloc/new, is this true for all memory I use? I.e,…
Andreas Mieritz
  • 135
  • 2
  • 6
7
votes
2 answers

Mex function not updated after recompile

I have a simple mex function, which calls another C++ function from a library. I compile the source with mex -cxx mymexfunction.cpp -I/some/include -L/some/lib -lmylib The mylib library is dynamic (.so) and is linked itself against some other…
Martin
  • 12,408
  • 6
  • 34
  • 30
7
votes
4 answers

Explicitly setting the value of a pointer

I am writing a mex-file (using C++) that will accept a memory address as an input, and operate on data at that memory address. Because I am forced to use MATLAB as my environment, my program can only accept MATLAB data types as inputs (char, bool,…
Brian
  • 73
  • 5
7
votes
2 answers

Bug? MATLAB MEX changes the kind of the default logical

When interfacing a piece of Fortran 2003 (or above) code with MATLAB by MEX, I am surprised to find that MEX changes the kind of the default logical. This is fatal, because a piece of perfectly compilable Fortran code may fail to be mexified due to…
Nuno
  • 256
  • 1
  • 11
7
votes
4 answers

Embed a function from a Matlab MEX file directly in Python

I am using a proprietary Matlab MEX file to import some simulation results in Matlab (no source code available of course!). The interface with Matlab is actually really simple, as there is a single function, returning a Matlab struct. I would like…
OlivierB
  • 313
  • 3
  • 14
7
votes
3 answers

How to stop Matlab crashing on (wrong) mex-file execution with CUDA functionality

I'm currently developing a mex-file with CUDA functionality to be used in MATLAB. When I'm doing something wrong (e.g. wrong pointers or something like that), MATLAB always crashes (windows prompts me to end, send the report for mathworks or attempt…
tim
  • 9,896
  • 20
  • 81
  • 137
7
votes
1 answer

How to return a float value from a mex function, and how to retrieve it from m-file?

I understand that all the returned values of a mex function are stored in plhs array of type mxArray*. I want to return a value of type float. How can I do it? Some code examples on returning it from the mex function and retrieving it from the…
drox
  • 7,523
  • 4
  • 23
  • 34
1 2
3
98 99