Using codegen for reading data from a text file in C

3 visualizaciones (últimos 30 días)
Yuval Geyari
Yuval Geyari el 2 de Sept. de 2018
Editada: Kevin Chng el 3 de Sept. de 2018
I'm trying to write a code that reads numeric data from a text file and stores it in an array. Eventually, the code should be compiled as an '.exe' file using codegen. The problem is that C programming does not support "obvious" functions that can help me do it, such as fgets or str2num. Can anyone help me write a Matlab code that can eventually be compiled? The format of the data text file is mine to decide.
  3 comentarios
Walter Roberson
Walter Roberson el 3 de Sept. de 2018
Kevin, that is more like int2str. It does not handle negative numbers, and I suspect that it outputs an extra 0 when the value is not an integer.
The poster needs to read in numbers, not output them.
Kevin Chng
Kevin Chng el 3 de Sept. de 2018
Editada: Kevin Chng el 3 de Sept. de 2018
Okay, just realised that I see his question wrongly.

Iniciar sesión para comentar.

Respuesta aceptada

Yuval Geyari
Yuval Geyari el 3 de Sept. de 2018
Thanks everyone, I solved the issue by using the functions strtok and str2double, which are supported by C programming.

Más respuestas (1)

Walter Roberson
Walter Roberson el 3 de Sept. de 2018
https://www.mathworks.com/help/simulink/slref/coder.ceval.html
You should almost certainly be able to use anything in the standard C library, including sscanf, if you invoke it by coder.ceval or coder.extrinsic.
Now, there is the issue that the standard C library has no file system support. fgets is not truly part of C: it is an operating system function such as might be defined by POSIX.
This basically means that if you are writing a C program for deployment running a literal toaster, that it is not at all an error that the compiler does not permit you to access files. Or more modernly, it is not at all an error that your arduino does not support files. Raspberry Pi is a hosted implementation and does support files, but that is outside of C proper.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by