Simulink S function builder Output Code

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)

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

I agree. Using an Embedded MATLAB Function would be a lot easier.
Thanks for the Quick response!
Initially I was instructed to use the S-function builder but after doing some research I thought that a MATLAB fcn or Embedded MATLAB function would be more applicable. I am not familiar with how to set up the MATLAB fcn or Embedded MATLAB function. Any guides as to how I would be able to set it up so the outputs are just the X-displacement values?
Thanks so much for your help!
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.
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?
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?
Thanks for the help! I think since we are using MATLAB R2009a we don't have the Interpreted MATLAB Function block (just MATLAB fcn). We edited the TT_Tools_demo m-file to output the X displacements and removed alot of the code we were not using. We then called that function from the MATLAB fcn block in Simulink. We are able to incrementally build our simulation with that MATLAB fcn up to a point. We get a Model Error that says, "Error building Real-Time Workshop target for block diagram 'TT_Tools_sim_0608'. MATLAB error message: Error: Real-Time Workshop Error in block: "TT_Tools_sim_0608/MATLAB Fcn", block type "MATLABFcn": Not yet supported by Real-Time Workshop."
We updated our copy of R2009a and the Real-Time Workshop form the MATHWORKS site but the error still appears. Any ideas as to why the block type is not supported by RTW and what might be done to solve the issue?
Thanks so much for your help!
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. :)

Sign in to comment.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Asked:

on 6 Jun 2011

Community Treasure Hunt

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

Start Hunting!