How can I have Matlab Simulink autocode program read data from an external file?
5 views (last 30 days)
Show older comments
I have a Simulink model that contains calculations that I want to have it perform on different external data files. I want to generate C-code autocode from the model that I can compile into an EXE application file. I would like the application to prompt me for the input data file to read and an output file to write the results. I'm having difficulty figuring out what I need to put in the model for the I/O interface and how to type cast all the I/O variables. I would really like some kind of an example for doing simple external file I/O just to get me started but i haven't been able to find anything in my searches yet. Any hints or suggestions would be appreciated - thank you!
2 Comments
Piyush Kumar
on 13 Mar 2025
@Glen Tallarek, please check the 'From File' block, which is used to load data from a MAT file into a Simulink model, and the 'To File' block, which is used to write data to a file.
Answers (1)
Suman
on 8 Apr 2025
Edited: Suman
on 8 Apr 2025
Hi Glen,
In order to promt for user input in your compiled application, you will need to incorporate custom C/C++ code in the generated code since the MATLAB 'input' function is not supported for code generation.
Once you take the filename input using your C/C++ code, you can use one of the the MATLAB's various functions to read and write the data to text files(CSV, TXT, M). To call these MATLAB functions from C/C++ code, you can use MATLAB External Interfaces.
This is one of the approaches you can follow.
See Also
Categories
Find more on Simulink Coder 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!