MATLAB: How to make the code run faster

csvreadsize;xlsread

Good Morning,
this line apparently takes 21s to run:
s = size(xlsread(filename, 1, 'F:F'))
It is supposed to count the number of rows of an Excel sheet. It is actually a .csv data file, but the values are not separated by commas but by semicolons, so I cannot use the Matlab function ''csvread''. Is there maybe a faster or more elegant way to do this? And is it the ''xlsread'' oder the ''size'' that takes so long…?
Thank you in advance

Best Answer

If all you want to do is count the number of lines in the file, then parsing it is a waste of time:
numlines = sum(fileread('modified.csv') == char(10))
is probably the fastest way to get the number of lines.
If you do want to actually access the content of the file, then readtable is probably the easiest way to get it. This does not involve transitioning through excel so should be much faster than xlsread. readtable can usually detect the file format on its own. If not you can give it hints.