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

5 次查看(过去 30 天)
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 个评论
Piyush Kumar
Piyush Kumar 2025-3-13
@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 2025-3-13
编辑:Glen Tallarek 2025-3-13
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).

请先登录,再进行评论。

回答(1 个)

Suman
Suman 2025-4-8
编辑:Suman 2025-4-8
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.

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by