legacy_code
Show older comments
I'd like to use some legacy c with simulink. I've worked through the help but have some questions:
1. How do I add more than one function to an S-function. 2. How do I link a global C variable to a matlab variable r/w - so that I can see what the functions do to the global variable and modify the global from simulink?
Answers (1)
Kaustubha Govind
on 26 Sep 2011
1 vote
- You can define any number of functions in an S-function file (or have them in separate C-files that you can link into the S-function binary), as long as you have defined the required mdlXXX functions. What determines the operation of the S-function is where/how you call into the legacy functions. For example, you could call one from mdlInitializeSizes and the other from mdlOutputs, or both from mdlOutputs in whatever order you choose, etc.
- Firstly, note that it is dangerous to use C global variables in an S-function that determine the output, because if you have multiple instances of the S-function in the same model, they can write over each other. The best way to maintain instance-specific data that is accessible to all the mdlXXX methods is to create a DWork variable. Secondly, there is no direct way to link a global C variable to a MATLAB workspace variable. However, you can access the C global variable directly in S-functions, just like you would in any other C code. You could have ssPrintf statements in the S-function mdlXXX methods to print out the value of the variable before/after function calls.
Categories
Find more on Block and Blockset Authoring 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!