- Wrap the void Function: Create a wrapper function that takes inputs and returns outputs, and inside this wrapper, call the original void function. The wrapper function will handle the interaction with the S-Function.
- Define Global Variables: Use global variables to pass inputs to the void function and retrieve outputs from it.
- Create the S-Function: Use the legacy code tool to create the S-Function for the wrapper function.
How to create a S-function for targetlink generated code .
79 views (last 30 days)
Show older comments
I tried legacy tool for generating S-function which I was able to do .
eg. when I have a function with input parameters and return type well defined there is no issue .
When the function looks like this==>int sum (int a, int b) inside sum.c.
But in Targetlink generated code the main function is a void function.
eg. void sum (void), where i am struggling to create S-Function.
Can you please let me know any workaround for the same.
Regards
Sumit
0 Comments
Answers (2)
arushi
on 12 Aug 2024 at 6:33
Hi Sumit,
You may try creating an S-Function by managing the inputs and outputs through global variables or by wrapping the void function to fit the S-Function interface.Workaround Steps
Hope this helps.
0 Comments
Sahas
on 20 Aug 2024 at 10:14
As per my understanding of the question, you would like a way to generate “S-function” using “legacy tool” for a function whose “return” and “argument” parameters are of “void” type.
I was able to create a small example for this, please follow the steps below to create the “S-function” with required specifications:
- Create two code files named "sum.c" and "sum.h" that contain a void function and the headers, respectively.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1756124/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1756129/image.png)
- Generate the S-function using “legacy code tool”.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1756134/image.png)
- A Simulink model will open with the required “void” type block.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1756139/image.png)
To know more about using the “legacy code tool”, it’s integration with C/C++ code and a few examples, refer to the following documentation links:
- https:/www.mathworks.com/help/simulink/slref/legacy_code.html
- https://www.mathworks.com/help/simulink/sfg/integrating-existing-c-functions-into-simulink-models-with-the-legacy-code-tool
- https://www.mathworks.com/help/rtw/ug/legacy-code-tool-examples.html
I hope this is beneficial!
0 Comments
See Also
Categories
Find more on Code Generation for Custom Blocks 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!