call MEX-Funcion in a simulink userdefined block

Hi, I have a MEX-function that have several inputs and 2 outputs. In addition I creat a simulink model whit several blocks. I need to call the MEX-Function whit these blocks in the model. how can I do it? It's important for me that arrive to minimum executing and calling time.

Réponses (1)

Kaustubha Govind
Kaustubha Govind le 30 Avr 2013

1 vote

MEX-functions can essentially be called just like any other MATLAB function. Please see the available MATLAB Function blocks to find a Simulink block that can be used to execute a MATLAB function.

6 commentaires

omid jab
omid jab le 1 Mai 2013
Modifié(e) : omid jab le 1 Mai 2013
thanks dear Kaustubha,I have a MEX-file only. this Mex-file name is "Screen.mexw64", I fond it in psychtoolbox (www.psychtoolbox.org). but I don't have "Screen.c" and "Screen_wrapper.c". I can call it from "MATLAB Fcn" block. but this block is not fast enough. How can I call "Screen.mexw64" as a C-function in "S-Function Builder" for creat a "Level2 matlab s-function"? I use below cod in "s-function builder" block. this cod generate "example.c" and "example_wrapper.c" and "example.mexw64". but when I run my model (tha included genarated MEX-file), MATLAB show me a "matlab system error" window.
*typedef int (__cdecl *MYPROC)(int, mxArray[], int, const mxArray*[]); int nlhs=1; mxArray plhs[2]; int nrhs=1; const mxArray *prhs[2];
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
O1 = 1;
hinstLib = LoadLibrary(TEXT("Screen.mexw64"));
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "mexFunction");
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
// (ProcAdd) (nlhs, plhs, nrhs, prhs);
(ProcAdd) ('OpenWindow',0);
}
fFreeResult = FreeLibrary(hinstLib);
}
if (! fRunTimeLinkSuccess)
printf("unable to call the other MEX file\n");*
Omid: Considering that you don't have the actual source code to the MEX-function, I don't see any advantage in calling into the MEX-function via a C S-function - I would wager that the performance will be equivalent to using the MATLAB Function block. Btw, I think the standard way to call MEX-files from C is using mexCallMATLAB. I don't know how LoadLibrary needs to be configured to do this.
omid jab
omid jab le 2 Mai 2013
dear Kaustubha,I am an amuteure user and did not know " mexCallMATLAB". this is more suitible and mor simple than my prior method. thanks for introduce it to me. but I have a new problem: my mex-file is called in matlab as [a,b]=Screen('Openwindow',0). I know "nlhs = 2, nrhs = 2, prhs=['OpenWindow',0]". but I dont know, how must write it in C.
Please look at the examples listed at the bottom of the documentation page that I previously linked for mexCallMATLAB.
omid jab
omid jab le 6 Mai 2013
I studied all of them, but no understand how can creat a pointer such as prhs=['OpenWindow',0]".
omid: You need to create mxArray's for both - use mxCreateString for the first and mxCreateDoubleScalar for the second.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange

Produits

Question posée :

le 27 Avr 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by