Stuart’s MATLAB Videos

Watch and Learn

Reading Whole Lines of Text from a File

Sometimes the text files I need to read have an irregular format, i.e. it changes row-to-row. MATLAB file reading functions that expect columns of data (e.g readtable), need a uniform format to work. So if this is not the case, you need to read the file line-by-line, then split the lines/rows up into separate elements inside MATLAB. This gives you the ability to handle structure of any complexity. My colleague Jeremy, who works on MATLAB file I/O, recommended these parameters to use as the safest way to read in text files a line at a time:
Format = '%[^\r\n]', Delimiter = '' and EndOfLine = '\r\n',
In this code-along style video, I try to read a large text file using this method and making use of these functions:
  • textscan
  • contains, split, exractBefore
  • datetime
Play the video in full screen mode for a better viewing experience.
  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.