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
Video Player is loading.
Current Time 0:00
Duration 29:58
Loaded: 0.56%
Stream Type LIVE
Remaining Time 29:58
 
1x
  • Chapters
  • descriptions off, selected
  • captions off, selected
  • en (Main), selected
    Play the video in full screen mode for a better viewing experience.
    |
    • print

    Comments

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

    Loading...
    Go to top of page