Simulink S function builder Output Code
Show older comments
I am looking for some help to properly code my output in the S function builder. I have been trying a few different approaches and continue to get MEX compiler errors. Any guidance would be helpful.
Some more details:
The project I am working on is to embed the TT_Tools_demo.m function (can be found on Matlab File Exchange) into a Simulink program so that the outputs of the function are the X-displacements. I would like to be able to use these measured displacements (by Tracking Tools) by multiplying them with a constant to cause a motor to rotate in response to the displacement.
Any help is greatly appreciated!
PS: If the Embedded MATLAB Function or MATLAB fcn blocks are easier to use for this application I can use them.
Answers (1)
Kaustubha Govind
on 6 Jun 2011
0 votes
TT_Tools_demo.m is a MATLAB function, so it cannot directly be called from a C-MEX S-function. Is there a reason you would like to create a C-MEX S-function to call into this function? Why not just use an Embedded MATLAB Function or MATLAB fcn that you already seem to be aware of?
7 Comments
Arnaud Miege
on 6 Jun 2011
I agree. Using an Embedded MATLAB Function would be a lot easier.
Kunal
on 6 Jun 2011
Kaustubha Govind
on 6 Jun 2011
Here is an example using the Embedded MATLAB Function block: http://www.mathworks.com/help/toolbox/simulink/ug/f6-130172.html (now called the MATLAB Function block). The (Interpreted) MATLAB Fcn block (see http://www.mathworks.com/help/toolbox/simulink/slref/interpretedmatlabfunction.html) needs a function that takes a single input and generates a single output, so you need to modify TT_Tools_demo.m for this format.
Kaustubha Govind
on 8 Jun 2011
From Kunal:
I am using MATLAB 2009a, so I went to the old documentation and followed the Embedded MATLAB Function example there. I was able to successfully build and run that simulation. I am now working on trying to embed TT_Tools_demo into the function block. I keep getting a lot of different errors, many of which I don't understand. Many of them have to do with defining variables, but I thought they were already defined in TT_Tools_demo.
I don't have a lot of experience working with MATLAB or Simulink so I am learning a lot of this as I go along. Really all I need out of the MATLAB Function block is a real-time value for the displacements in the X-direction. I feel that maybe a lot of the errors that are shown during the build may have to do with auxiliary tasks of the function. Do you know exactly where in TT_Tools_demo the X-displacement is found? And how I may be able to change TT_Tools_demo to extract only that value for the output in my Embedded MATLAB Function?
Kaustubha Govind
on 8 Jun 2011
Perhaps using the Interpreted MATLAB Fcn is a better option for you, since Embedded MATLAB only supported a small subset of the MATLAB Language and you need to do extra work to get the function working with that block. I'm not sure how you could get the X-displacement from the function, but perhaps you could contact the author about it?
Kunal
on 8 Jun 2011
Kaustubha Govind
on 8 Jun 2011
Ah! I hadn't realized that you intended to generate code from your model. In that case, the MATLAB Fcn block is not supported. I'm sorry to say this, but you will need to use the Embedded MATLAB Fcn block for code generation purposes. However, since the Embedded MATLAB block only supports a limited functionality (see http://www.mathworks.com/products/matlab-coder/description2.html), you need to modify your code a little further.
From looking at the source code for TT_Tools_demo (http://www.mathworks.com/matlabcentral/fileexchange/26449-tracking-tools-optitrack/content/TT_Tools_demo.m), it looks like the crux of it is to call into a shared library using loadlibrary and calllib. You will need to replace this with use of eml.ceval (you can look for examples in the documentation that ships with your product - the online documentation has changed slightly with the new MATLAB Coder API).
PS: The Interpreted MATLAB Fcn is just the new name (starting R2011a) for MATLAB Fcn. :)
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!