Calling mex function from within Simulink

조회 수: 34 (최근 30일)
Octav Chipara
Octav Chipara 2012년 4월 29일
Hi,
I have been struggling to get a matlab function to compile in Simulink. I managed to compile it with the Coder and I have a mex file handy. However, in simulink, the same function does not compile using the Simulink coder. It is complaining about some dependencies which seem to be present when invoking the coder. Is there a simple path to invoke the mex file created with the Coder?
Thanks, -- Octav
  댓글 수: 1
Friedrich
Friedrich 2012년 4월 30일
Simulink uses S-functions which is a different principle as MATLAB uses mex files. Since you have a nice mex file, why not simply use a matlab function block and call the mex from there?

댓글을 달려면 로그인하십시오.

채택된 답변

Kaustubha Govind
Kaustubha Govind 2012년 4월 30일
As Friedrich mentioned in his comment, MATLAB MEX-functions can be invoked from a MATLAB Function block, but they are not supported for code-generation. You will need to convert your MEX-function to an S-Function and call that using an S-Function Block. Typically it is recommended that you write a TLC file to inline the S-function in generated code, but even without a TLC file, you can generate code by checking the Support: non-inlined S-functions checkbox in the Configuration Parameters window. Note that the generated code is not very efficient for non-inlined S-functions because it needs to dispatch calls to the S-function loaded as a shared library, and passes around a large object called the SimStruct.
  댓글 수: 1
Martijn
Martijn 2012년 5월 29일
Dear Kaustubha, How can I convert the MEX-function to an S-function?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Martijn
Martijn 2012년 5월 29일
I am running into a simular problem as described above. I am looking for more specific information on how to construct an S-function from a large collection of C++ routines, headers etc.
I need to implement a nonlinear solver as a Simulink S-function such that it is code-generation for XPC-target and Windows-target code-generation is possible. I am using the IPOPT solver (<http://www.coin-or.org/download/source/Ipopt/)>, which consist of a large set of C++ routines, headers etc.
This is what I've tried so far: • Since I already have a mex-file (OptiToolbox Opensource) I thought it would be convienent to use that inside a simple 'Matlab Function'-block. This works in Simulink, but the code generator cannot compile the Simulink diagram to a real-time executable code. • Creating an S-function in which I call the .mex function. This code cannot be compiled since the .tlc file is missing.
After some search on fora and Matlab central, I found out that I should create a C MEX S-function and compile it. To make life easier, Simulink comes with an S-function builder in which libraries etc. can be added. However, I failed in constructing one and it is driving me crazy.
Who has experience in using a nonlinear solver as an S-function in Simulink?
  댓글 수: 1
TAB
TAB 2012년 5월 30일
See my answer of your question
http://www.mathworks.com/matlabcentral/answers/39764-convert-a-mex-function-to-an-s-function

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by