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.