How to identify the input and output in a c code generated from Simulink model

12 views (last 30 days)
With the help of the simulink coder, I could genrate a c code for my simulink model.
Now, I want to know how and where to find the inputs and outputs presen in the c code, so that a cusomized inputs can be passed to the model.
Please let me know if any additional information is required.

Accepted Answer

Divya Yerraguntla
Divya Yerraguntla on 6 Feb 2020
Hi Vivek,
In order to provide customized inputs you need to change the input source blocks used in the model during simulation to Inports(say In1, In2 etc.) and then generate the code. Now when you look at the generated (model_name).c file you will be able to find the void (model_name)_step(void) function which is called at every time step and contains the main dynamics of the model. This is the function where you can read inputs from external sources at every time step and produce output.
In the (model_name).c you will notice input variables as test_U.In1, test_U.In2 etc. We can read inputs and display outputs in this function as shown below:
scanf("%lf %lf",&(test_U.In1),&(test_U.In2)); %% lf for double datatype
printf("Input: %lf %lf\n",test_U.In1,test_U.In2);
%% Model Dynamics(computations on test_U.In1,test_U.In2)
printf("Ouput: %lf\n",test_Y.Out1);
Now on compiling, buidling and finally running the executable it will prompt you to provide input for all the time steps in one go and once you give that, you will see the output as well.
Hope it helps!

More Answers (1)

Vivek Anantharaman
Vivek Anantharaman on 10 Feb 2020
Hi Divya,
Thanks for the information.
By using the input and output ports, I could see the inputs and outputs 'DTX_U.In1' , 'DTX_Y.Out1' in the generated code. I added the printf and scanf statements in the DTX.c file and tried to run the .bat file for a new executable.
But when I run the .bat file, there is no new executable created, the command window is crashing without any prompt.
Please find the attachment for the .bat file and the generated code file for reference.
Could you please let me know what are the prerquisite to successfully run the .bat file to generate the new executable.
Thanks for your support.
Best regards,
Vivek.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!