MATLAB: How to set up a Simulink model such that the generated code imports data from text files on the embedded target, and then runs the model, using incoming data, multiple times with different signals and parameters in Simulink 8.0 (R2012b)

carloEmbedded Codermontersim

I would like to test the C code generated from my Simulink model by running it on the embedded target. I would like the generated code for the model to be executed several times with different parameters or input signals, with these parameters and signals being read from text files on the embedded target.

Best Answer

To test generated code on an embedded processor with different signals and parameters, the best approach would actually be to use the Processor-in-the-Loop (PIL) feature of Embedded Coder. PIL mode allows you to run the code on the embedded target, while using MATLAB and Simulink to simulate sensor inputs to the system. For Embedded Coder 6.3 (R2012b), documentation on this feature can be found at the following link:
To answer the original question more directly, there are two components to this issue:
1. Reading data from a text file (or other file) in the generated code at runtime on the target processor.
2. Changing the signals or parameters of the model in the generated code using external values.
Regarding point #1 (reading from a text file on the target processor): as of version 6.3 (R2012b), the Embedded Coder product does not currently generate code to read from a text file or MAT-file at runtime. While this may sound like a trivial capability, there are actually many obstacles to its implementation. For example, the generated code would need to contain extensive error checking for incorrect data formats and instructions on what to do if the operation fails, and there is no one solution that would be satisfactory for all cases. The best way to implement reading from text files in the generated code would be to use the Custom Code pane of the model’s Configuration Parameters to add code that reads in the text file according to the data format that you have, and defines what to do if the operation fails.
Regarding point #2 (accessing signals or parameters in the generated code): the way to go about doing this would be to use the C API feature of Simulink Coder. Essentially, this is enabled by going to the model’s Configuration Parameters, and choosing “C API” under Code Generation->Interface, and clicking on the Interface drop-down box. In the generated code, there will then be two additional files, ‘model_capi.c’ and ‘model_capi.h’, which provide access to pointers to the signals, parameters, etc. in the generated code.
For Simulink Coder 8.3 (R2012b), documentation on data exchange using the C API can be found at the following link: