"From File" block does not update MAT files after compiling my model with Simulink Compiler

1 view (last 30 days)
I have a Simulink model that loads data from MAT files using 'From File' block, updates the data, and saves it to the same MAT files at the end of the simulation.
  • When the model is run in Simulink, there are no issues and the data in the MAT files is updated as expected.
  • When I run the compiled model, the MAT files are not updated. Rather at the end of the simulation, they contain the same data that was there when the model was being compiled.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Feb 2025
Edited: MathWorks Support Team on 4 Feb 2025
This behavior is expected. When code is generated for the 'From file' block, the data in the target file is inlined to optimize it, i.e., hard-coded. Thus, any changes made to the target file after code generation are not honored by the compiled model. 
For example, suppose you have a MAT-file containing a variable "A" with a value of 2, and a Simulink model that adds 1 to "A" and updates the MAT file. 
  • When the model is run in Simulink, it will read "A = 2", increment by 1, and save the updated "A" to the MAT file. Running the model afterward will continue to increment A by 1.
  • Whereas, when the model is compiled, the value of "A = 2" is hard-coded, so the "From File" block reads the hard-coded value rather than reading from the file. Running the model afterward will continue to use hard-coded "A = 2" and increment it by 1.
For more limitations, please refer to the following documentation page by executing this command in the MATLAB R2020a command window:
>> web(fullfile(docroot, 'simulink/slref/fromfile.html?searchHighlight=fromfile'))
If you wish to change the data input to a compiled model after every run, please use an 'Import Block'. To view the relevant documentation, execute this command in the MATLAB R2020b command window:
>> web(fullfile(docroot, 'simulink/slref/inport.html'))
and supply the MAT-file as a root-level input. For more information, execute this command:
>> web(fullfile(docroot, 'simulink/ug/load-data-to-root-level-input-ports-1.html'))
This way, the code is compiled in such a way that the changing external data is honored.
Please follow the link below to search for the required information regarding the current release:

More Answers (0)

Categories

Find more on Prepare Model Inputs and Outputs in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!