Read text file line-by-line in deployed application

I am trying to read a large TXT file in my deployed application, but I am encountering memory limitations. Therefore, I would like to read in the TXT file line by line, but the function 'fgetl' is not supported for C/C++ code generation with MATLAB Coder. 
Is there any other way to read a TXT file line by line?

 Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 25 Jan 2025
Modifié(e) : MathWorks Support Team le 31 Jan 2025
This can be achieved using the functions "fgets" or "fgetl". The code generation support for these functions was introduced in R2019b. To access the release-specific documentation for "fgets", execute the following command in the MATLAB R2019b command window:
>> web(fullfile(docroot, 'matlab/ref/fgets.html'))
For "fgetl", use the following command:
>> web(fullfile(docroot, 'matlab/ref/fgetl.html'))
For C/C++ deployment of your MATLAB code in releases prior to R2019b, you will have to write your own implementation of this functionality.
One possible solution to read a TXT file line-by-line could be as follows:
function main %#codegen fid = fopen('myfile.txt'); lne = myfgetl(fid); while ~isempty(lne) fprintf(1,'%s',lne); fprintf(1,'%s',sprintf(' ')); lne = myfgetl(fid); end fclose(fid); end function line = myfgetl(fid) line = ''; c = fread(fid,1,'char=>char'); while ~feof(fid) && ~strcmp(c, sprintf(' ')) if ~strcmp(c,sprintf('\r')) line = [line,c]; end c = fread(fid,1,'char=>char'); end end
Please follow the link below to search for the required information regarding the current release:

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB Coder dans Centre d'aide et File Exchange

Produits

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by