Changes in the function "textscan"?

3 Ansichten (letzte 30 Tage)
Christian Keidel
Christian Keidel am 4 Apr. 2022
Beantwortet: Rahul am 20 Jun. 2025
Hello,
i use the textscan function to read the first 500 lines (the header of my measurement data is really big) of a lot of text files, to check which file i can evaluate. I used the Matlab Version R2019a. To read the first 500 lines of the files the function needed some µs (local drive) and 0.5 seconds (network drive).
Recently i changed to R2021b and the same function now needs on my local drive nearly 0.5 seconds and on the network drive 12 seconds.
After some tests with both matlab versions, it looks like that the new Matlab version reads the whole file and than saved the first 500 lines while the old version reads only the 500 lines.
fid = fopen(path);
tic
data = textscan(fid, '%s', 500)
toc
fclose(fid);

Antworten (1)

Rahul
Rahul am 20 Jun. 2025
I understand that you wish to understand the regression in the speed of the 'textscan' function. The tested the code shared by you in the question and observed that it reads the first 500 words of a text file not the first 500 lines. To read the first 500 lines, a 'Delimiter', '\n' would be added.
There are 3 other ways to do this:
  • Using 'fgetl' function
  • Using 'readlines' function
  • Using 'fcanf' function (with approximation of 10000 words)
I have compared the performance of the 4 ways on a text file with lorme ipsum text and obtained the following results:
  • 'textscan': 0.000871 seconds
  • 'fgetl': 0.0021 seconds
  • 'readlines': 0.00438 seconds
  • 'fcsanf': 0.0010 seconds
These are the averages of 5 readings while the text file was placed in a network drive.
These results have been obtained on MATLAB R2025a, hence the suspected regression of the 'textscan' function cannot be observed in the latest releases. Hence, upgrading to the latest version of MATLAb might resolve the issue.
I am sharing the code file used for obtaining results and the text file used.
The following MathWorks documentations can be referred:
Thanks.

Community Treasure Hunt

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

Start Hunting!

Translated by