Help Me in Debugging C Code to S-Function MATLAB

4 views (last 30 days)
Hello Community,
I have .c code file ( for model predictive control) which I received from my professor. That code was written by one of student who is now graduated and I don't have any contact with him.
I am facing problem in compiling that code. I didn't have any experience about C programming language as well as S Function.
After watching few tutorials on Youtube I know how to call using Legacy Tool.
In def.OutputFcnSpec we need to specify our output but unfortunately I am not able to do it and contineously receiving error. I know there is no fault in the code but only mistake I am doing is that I can't able to define the def.OutputFcnSpec correctly.
I have attached the zip file to this question in which I want to compile mpcsolver.c file to use in S-Function block. In the zip file there is .m file which I code to call the legacy function. Please Help me.

Answers (1)

Sayan on 4 Dec 2023
Hi Muhammad,
I understand from your issue that you need to create a ".mexXXX" (MATLAB executable) file from the source files "mpcsolver.c" and "EKFSolver.c" using the "Legacy Code Tool".
After looking at the source file, I could not find any "add" function, which is specified as "OutputFcnSpec" in the "code_to_call_s_function.m" file for the "Legacy Code Tool." If you are using a handwritten S-function, you still need to define the "add" function and make a call to it from "mdlOutputs".
Alternatively, as you have the "mpcsolver.c" written as "Level-2 MATLAB S-function", you can create the ".mexXXX" without the "Legacy Code Tool" by using the following command:
mex mpcsolver.c
This will create the MATLAB executable, which you can use in the S-Function block to create the desired block in the Simulink model. However, you need to specify the parameters of the S-function in the "S-function parameters" field, separated by comma.
You can find the following documentations on "Integrate C Functions Using Legacy Code Tool" , "S-Functions Incorporate Legacy C Code","Write Level-2 MATLAB S-Functions" and "S-Function" block for more useful information on them.
Hope this helps in resolving the issue.


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!