f



Call Matlab functions from mex (matlab function's input is string)

Here is my mex code:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
    char            errBuff[1]={'A'};
    //create mxArray pointer
    mxArray *mxerr;
    //copy errBuff to mxArray
    mxerr = mxCreateString(errBuff); //initialize mxArray with errBuff char array, return to pointer
    //call matlab function
    mexCallMATLAB(0,NULL, 1,mxerr, "errorCatcher");

    //destroy mxArray
    mxDestroyArray(mxerr); //destroy mxArray
    printf("destroyed mex array\n");
}//end of mexFunction

errorCatcher accepts string inputs. The code compiles but matlab crashes when I run it. I think the error is segmentation fault. What is wrong with this code? I placed "hello worlds" and program crashes when I call the matlab function.

Btw, How do I set breakpoints in mex files?
0
Bilgehan
6/5/2015 12:15:19 PM
comp.soft-sys.matlab 211266 articles. 19 followers. lunamoonmoon (257) is leader. Post Follow

2 Replies
593 Views

Similar Articles

[PageSpeed] 24

"Bilgehan paray" wrote in message <mks3sn$q2s$1@newscl01ah.mathworks.com>...
> Here is my mex code:
> 
> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
>     char            errBuff[1]={'A'};
>     //create mxArray pointer
>     mxArray *mxerr;
>     //copy errBuff to mxArray
>     mxerr = mxCreateString(errBuff); //initialize mxArray with errBuff char array, return to pointer
>     //call matlab function
>     mexCallMATLAB(0,NULL, 1,mxerr, "errorCatcher");
> 
>     //destroy mxArray
>     mxDestroyArray(mxerr); //destroy mxArray
>     printf("destroyed mex array\n");
> }//end of mexFunction
> 
> errorCatcher accepts string inputs. The code compiles but matlab crashes when I run it. I think the error is segmentation fault. What is wrong with this code? I placed "hello worlds" and program crashes when I call the matlab function.


This line specifies that errBuff is a *single* character. It is NOT null terminated, so it is not a C-style string.

>     char            errBuff[1]={'A'};

But the mxCreateString requires a C-style null terminated string as input.  So when you call mxCreateString it searches for the ending null character '\0' and thus ends up searching beyond the end of the array into invalid memory, hence the seg fault.

The fix is to make sure you have a C-style null terminated string for errBuff. E.g.,

char            errBuff[]={"A"};

The use of the double quotes tells the compiler to construct a C-style null terminated string.  So errBuff will end up with two characters ... the 'A' and a null '\0'.  The use of empty brackets [ ] tells the compiler to make the errBuff array large enough to hold your specified characters and the added null character (i.e., let the compiler count characters for you).

James Tursa
0
James
6/5/2015 5:12:19 PM
"Bilgehan paray" wrote in message <mks3sn$q2s$1@newscl01ah.mathworks.com>...

(snip)

>     mxArray *mxerr;
            :
>     mexCallMATLAB(0,NULL, 1,mxerr, "errorCatcher");

Also, the level of indirection is wrong on the above line. It should be this:

mexCallMATLAB( 0, NULL, 1, &mxerr, "errorCatcher" );

The reason is that the function prototype for the 3rd argument is mxArray *prhs[], which boils down to mxArray **, but you are just passing a variable of type mxArray *.  So you need to add the address-of operator & to get that extra level of indirection.

James Tursa
0
James
6/5/2015 5:20:20 PM
Reply:

Similar Artilces:

Error while obtaining sizes from MEX S-function 'forward' in block 'dummy/Dynamic_Model/S-Function'. MATLAB error message:.
Hi to everybody, I created the file forward.dll using the command: mex -DWIN32 -output forward model_d.c model_s.c sdlib.c assigns.c contact.c derivs.c gmc.c impact.c inits.c invert.c lcp_solver.c lmdif_c.c mathtools.c object.c output.c pipetools.c readmuscles.c readtools.c sdfor.c spring_based.c wrapping.c gcvspl.c user_def.lib forward.lib acpp.lib qhull.lib solid.lib wrap.lib I need to run the command: set_param([subsystemname,'/','S-Function'],'function name','forward','parameters','[0],[0],[0],[0],[0],[0],[1 0 0 0 20],[0]'); ...

Embedded Matlab interface error : error calling MATLAB function 'sf'
Hi, I am using MATLAB 7.6 on a 64-bit machine. I am trying to run a mathematical model containing various user defined embedded matlab function that was previously 'built and run' using same matlab version but on a 32-bit machine. I am getting this error on my current machine setup "Embedded Matlab interface error : error calling MATLAB function 'sf'". I have already visited many matlab central pages yet I am unable to find a solution to this specific problem. N.B: I tried to configure a c compiler through mbuilder -seup on caommand line but it seems that there...

convert C-Mex S-Function to Matlab S-function
Hello, does anyone know if it's possible to convert s-function written in C to Matlab? Thanks Regards ...

Preventing Matlab's history from data entered through 'Input' function
Whether Matlab's history can be adjusted to prevent recording the data entered from the keyboard through 'Input' function during execution of a script? Thanks you very much in advance. ...

Calling Matlab functions from S-Functions in C...
Hi, I am trying to write an application in Simulink using S-Functions written in C language. I would like to know if I could call Matlab Array functions like 'var', 'median' etc from the S-Functions written in C. i.e., Is there a C interface for the Matlab functions? Any response would be appreciated. Thanks Pradeep. Pradeep wrote: > Hi, > > I am trying to write an application in Simulink using S-Functions > written in C language. I would like to know if I could call Matlab > Array functions like 'var', 'median' etc from the S-Functi...

Writing C MEX S-Function from existing Matlab C MEX function
I have an existing Matlab C MEX function (walkode.dll) that returns state derivatives when called by ode23s (walkode,...) from a regular Matlab script or the command line. I would like to incorporate this into a Simulink model, and have done so using a Level 1 M-file S-function, which called walkode in the mdlDerivatives subfunction. However this model runs very slowly. I would prefer to rewrite (recompile?) my .dll as a C MEX S- function (using sfuntmpl_doc.c I believe), but I am unsure as how to go about doing so. This is where my understanding (or more aptly, misunderstanding) of C,...

Matlab
Hi everyone, I'm trying to write code for the c2d function in Matlab - where the inputs are state matrices A, B, C, D, and a delta time-step. The outputs are [phi] and [gamma], the a and b matrices of the new, discrete state representation. I'm not sure where to start. Can anyone offer any suggestions? Thanks ...

How to call matlab functions directly from C++ /without calling matlab engine
How to call matlab functions directly from C++ without calling matlab engine. as it is described in this tutorial: http://www.codeproject.com/samples/matlab_cpp.asp I have tried to do something similar in Visual Studio 2005 [Matlab 7, r14] #include <mclcppclass.h> .... mwArray A, B, C; A = magic(mwArray(5)); B = transpose(A); .... but it doesn't compile. I got following errors: error C3861: 'magic': identifier not found error C3861: 'transpose': identifier not found etc... I was looking for header files with declarations of this functions in matlab directory...

Call a Matlab function from Java running in matlab.
Hi guys. I really apologize for asking this question, since I'm sure it is either basic or not possible, but I have been reading through the documentation and can't seem to figure this out, since the examples look like they just send and receive variables from the Java methods. Could anyone give me a short example of how to call a method from a Java object that causes some sort of action to be performed in MATLAB? For example, I would like to: 1) make a new object of MyClass from MATLAB called MyObject. 2) run MyObject.DoSomethingInMatlab() 3) have a statement in DoSomethingInMatlab...

call to matlab function from matlab GUI stalls
I have the following problem. I have created a simple Matlab GUI (for our purposes it can just be considered a Matlab function) which gathers some parameters and then invokes a perl script that in turn calls other perl and Matlab scripts to perform various calculations. If instead of using the Matlab GUI, I run the perl command from the unix prompt, the flow runs and concludes correctly. BUT if I invoke the flow from the Matlab GUI (function), then it runs correctly by calling various other perl and Matlab scripts, but then when it reaches the third Matlab function call, it consistently stalls...

How to use matlab functions as callback functions in dll's?
Hi All, one of the functions in a dll requests a callback function. I made a transcription of the c++ example in matlab. But using the @ to make a function_handle doesn't work. calllib('XISLDLL','Acquisition_SetCallbacksAndMessages', ... phAcqDesc.value, uint32(0), 0,... 0, @OnEndFrame_Callback, @OnEndAcq_Callback); function OnEndFrame_Callback(phAcqDesc) ~~~~~ ~~~~~ ~~~~~ function OnEndAcq_Callback(phAcqDesc) ~~~~~ ~~~~~ ...

how to call a Matlab function from within a MEX function written in C?
Hi all, I have a MEX function written in C, let's call it Afunc.cpp, and I have another Matlab function called Bfunc.m (it is not from the Mathworks, so it is not the pre-defined Matlab function) Is it possible to call this Bfunc.m from within Afunc.cpp? It is because the Bfunc.m is too complicated and it used a lot Matlab predefined function which I don't how to rewrite in C/Cpp. So a simpler way for me is to let Afunc.cpp call Bfunc.m... How to do that? Thanks a lot! Mike wrote: > Hi all, > > I have a MEX function written in C, let's call it Afunc.cpp, >...

Matlab's 'graycomatrix' function
I have one question: Is Matlab's 'graycomatrix' function actually in accordance with original Haralick's 1973 paper? I mean, are they supposed to be the same algorithm? If yes, I don't think Matlab did a good job on this. For example, try it on this matrix (i.e., 'image'): 0 0 1 1 0 0 1 1 0 2 2 2 2 2 3 3 This matrix is in Haralick's original paper and the results can be compared. Can anybody ellaborate if you used it? Thank you. "Bulent Bayraktar" <bayrakta@purdue.edu> writes: > I have one question: Is Matlab's 'graycomatrix'...

MatLab's 'movie' function
Hi! Using MatLab's 'movie' function you can play a movie recorded by the 'getframe' command. It is possible to specify the number of times the movie is played and the frames per second. Unfortunately, I've not found a way to stop the playback of a movie. Is it possible to, let's say I've specified the movie should be played 20 times, to disrupt the playback after an user interrupt?! Thanks a lot, Chris On Sat, 12 Feb 2005 10:40:06 +0100, Christoph ROLZ wrote: >Hi! > >Using MatLab's 'movie' function you can play a movie recorded by...

Web resources about - Call Matlab functions from mex (matlab function's input is string) - comp.soft-sys.matlab

Resources last updated: 1/25/2016 2:28:06 PM