How can I have Matlab Simulink autocode program read data from an external file?

5 views (last 30 days)
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
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.
Glen Tallarek
Glen Tallarek on 13 Mar 2025
Edited: Glen Tallarek on 13 Mar 2025
Hello @Piyush Kumar - thank you for the response! I will look at the documentation for those blocks and see if I can get them to work for what I want to do.
One thing I was thinking of was trying to read data from a text file (like a CSV, TXT or M file). It would be nice if there was some kind of function to do that so I wouldn't need to create an algorithm to parse the info (there might be such a function or block - I will ned to keep looking!).
Plus, I am autogenerating C-code from the model and then compiling it into a Windows application. Ideally I would like the Windows application to prompt me for an input data file and then have it read the data from the external file (hence my initial question). Then, after the data is processed, I want the application to write it out to an external output data file. So I need a solution for this situation (not just to load data into the Base Workspace for Simulation while in Matlab).

Sign in to comment.

Answers (1)

Suman
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.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!