mex

How to return a matrix structure from a mex function?

对着背影说爱祢 提交于 2019-12-10 12:14:19
问题 I have a struct that defines a 3d array, the size is known: struct uchar3 { unsigned char x, y, z; }; and I want to return it via mex function in order to use it in matlab like a three dimensional array, like an image. How can this be done? EDIT: This is apart of the function I use. foo(uchar3 **imagePtr, Mat Im){ unsigned char *cvPtr = Im.ptr<unsigned char>(0); for (size_t i = 0; i < Im.rows * Im.cols; ++i) { (*imagePtr)[i].x = cvPtr[3 * i + 0]; (*imagePtr)[i].y = cvPtr[3 * i + 1]; (

Call Matlab in C++ code - using methods in engine.h

北城余情 提交于 2019-12-10 11:56:24
问题 I write a C++ program whose only purpose is to call Matlab code. I have a main routine, which read data in a file (matrices with high dimension 90000*24) into C++ structures pass these structures to Matlab code launch the Matlab routine with these structures in argument get the output data from Matlab and store them in C++ structures In 2/, matrices are fields in a Matlab struct. The struct, say MATLAB_STRUCT has several matrix fields, MATLAB_STRUCT.Z1 , MATLAB_STRUCT.Z2 ,... and some float

Mex dynamic memory management issue with std::vector in linked external DLL; Segmentation error

浪尽此生 提交于 2019-12-10 11:44:15
问题 I am trying to create a mex file that interfaces MATLAB with an external C++ library that communicates with some hardware. An imported library and precompiled DLL (.lib and .dll) are provided by the hardware vendor for my version of VC++ and I was able to implement them in C++ without any issue. However, I ran into segmentation error at run time when the code is written as a mex(compiled with the same version of VC++). After some investigation with the VC++ debugger, the likely culprit seems

Matlab Mex file compile

左心房为你撑大大i 提交于 2019-12-10 10:02:16
问题 I'm trying to compile a mex file to use socket connection within matlab. The problem is that it is always saying that I don't have sdk or compiler installed. But I have installed visual studio 2010 express, visual studio 2012 express, visual studio 2012 professional and nothing is changing the error. Also I have installed the sdk. Is there anything that I could try? The matlab version is R2012b and I'm using windows 7, 64 bits. Thanks! 回答1: You can try gnumex, which lets you access the mingw

Passing C/C++ callbacks into the matlab engine

空扰寡人 提交于 2019-12-10 09:44:18
问题 I have a C++ file that: starts the matlab engine calls matlab_optimize() (a compiled m file that runs one of matlab optimizers internally) prints the result stops the engine and quits This works fine. I now want to change the second line into calls matlab_optimize(obj_fun) Where obj_fun() is a function defined in my C++ code which itself will callback into other code. Essentially I want the matlab optimizer used internally in matlab_optimize to use my supplied function pointer as the

Multithreading with Matlab

折月煮酒 提交于 2019-12-09 17:48:28
问题 I'm working on a project on Matlab where we have to optimize the performance, and I was thinking about parallelizing a couple of function calls that were made from a .m file. The idea was simple, from a Matlab file (.m) call a C file compiled as MEX, and from that C file, create a couple of threads and call back the matlab functions from each thread. The theory works, I can create the threads, and I can also call the matlab function, the problem is that I cannot call the matlab function from

Embed a function from a Matlab MEX file directly in Python

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 17:28:31
问题 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 to know if there is any way to call this function in the MEX file directly from Python, without having to use Matlab? What I have in mind is for example using something like SWIG to import the C function into Python by providing a custom Matlab

How to use CMake and Visual Studio 2010 (64 bit) to build a MATLAB R2011a (64 bit) mex file?

心已入冬 提交于 2019-12-08 01:46:53
问题 I would like to write a CMakeLists.txt such that CMake writes a Visual Studio 2010 (64 bit) solution file to build a mex function for MATLAB R2011a (64 bit) from C++ code example.cxx . I do not want to use MATLAB's compiler wrapper mex but set up the Visual Studio solution file such that Visual C++ links the relevant MATLAB libraries. example.cxx has no dependencies except for the MATLAB libraries that are necessary for mex files. CMake 2.8.7 is set up correctly such that it uses the 64 bit

How to pass an integer array from matlab to mex?

◇◆丶佛笑我妖孽 提交于 2019-12-07 15:44:34
问题 I want to pass an integer array from matlab to mex. The array is for example a=[1 2 3 4]. I wrote the following code: #include <math.h> #include <stdio.h> #include <stdlib.h> #include <mkl.h> #include "mkl_vml.h" #include "mex.h" #include "matrix.h" #include "mkl_vsl.h" /* main fucntion */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int n, *a; /* make pointers to input data */ n = (int)mxGetScalar(prhs[0]); a = (int *)mxGetData(prhs[1]); mexPrintf("a[0]:%d

Memory leak in MATLAB > MEX file > managed DLL

若如初见. 提交于 2019-12-07 14:47:28
问题 My MEX file is written in C++/CLI and calls a DLL written in C#. When gcnew'ing an object, shouldn't it be garbage collected when the mexFunction returns? Its references should be lost but nothing seems to be garbage collected... each call to the mex function increases MATLAB's memory allocation (and no, the memory is not used for MATLAB variables). I've experimented with creating a large dummy value with narrow scope and when stepping through the MEX file I can see the memory allocated and