Skip to content
MathWorks - Mobile View
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
MathWorks
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out

Videos and Webinars

  • MathWorks
  • Videos
  • Videos Home
  • Search
  • Videos Home
  • Search
  • Contact sales
  • Trial software
2:33 Video length is 2:33.
  • Description
  • Full Transcript
  • Related Resources

How to Import Data from Files Programmatically

Learn how to import data programmatically in MATLAB® by creating a script using the Generate Code option in the Import Tool, or by writing code from scratch. This video shows how to use import functions such as readtable, readmatrix, and readcell .

This tutorial focuses on spreadsheets, but MATLAB supports a wide variety of file types. The broad principles in this video can be applied to other supported file types as well.

Hello and welcome to another MATLAB tutorial.

Let’s say we have a bunch of data sets and we want to import them the same way every time but using the import button over and over again is just an inefficient use of our time. How would we go about automating our import?

Well lucky for us, MATLAB lets us import data programmatically. Now there are two main ways to do this you can import using the import tool and then press generate code and a script will be generated.

Or, you can write the code from scratch. In the documentation you can find a list of supported file types, as you can see there’s a wide variety from videos to photos to music, this tutorial just focuses on spreadsheets, but the broad principles can also be applied to importing those as well.

The import and export functions for each file types we’re focusing on are listed here. Now an exciting development of 19a is that you no longer need to use functions such as CSVread, Dlmread, Xlsread. Instead spreadsheets with clear delimitators all use the same import functions.

The import function is read and then whatever you want to read the spreadsheet in as. So, if you wanted to import the spreadsheet as a matrix, it’s readmatrix, if you want a cell array it’s readcell, if you want a table it’s readtable etc. etc.

So, if I type in readtable(filename) and hit run, boom the program imports and spits out a table. Note that you have to be in the same directory for just writing the file name to work. Alternatively, you can write the file location for more ease of use.

MATLAB is really good at guessing the options you want for your file. But if you want to change them, you can see your options by typing detectImportOptions(filename). It returns a struct of values, for more information about what all of these different variables correspond to, consult the documentation. But let’s say we want to alter the number rows we’re reading in. Well that’s this variable here, DataLines. So we assign the options to a variable opts and then we can change the number of lines by typing opts.DataLines and assigning a new value.

When we call readtable now, we can just do comma opts and then boom only the rows you want.

You can also set variable options in a similar manner by calling setvaropts. You just write Opts = setvaropts(opts, variable name, element to change, new variable). You can access the setvaropts documentation by clicking right here on in the importoptions struct.

Thanks for watching and happy coding. 

Related Products

  • MATLAB

3 Ways to Speed Up Model Predictive Controllers

Read white paper

A Practical Guide to Deep Learning: From Data to Deployment

Read ebook

Bridging Wireless Communications Design and Testing with MATLAB

Read white paper

Deep Learning and Traditional Machine Learning: Choosing the Right Approach

Read ebook

Hardware-in-the-Loop Testing for Power Electronics Control Design

Read white paper

Predictive Maintenance with MATLAB

Read ebook

Electric Vehicle Modeling and Simulation - Architecture to Deployment : Webinar Series

Register for Free

How much do you know about power conversion control?

Start quiz

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

3:22
How to Import Data from Spreadsheets and Text Files Without...

Related Videos:

6:21
Import Tool Enhancements for Text Files
2:09
How to Store a Series of Vectors from a for Loop
28:30
A Novel Method to Build Real-World Drive Cycles from...
33:26
Developing Forecast Models from Time-Series Data in MATLAB...

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (简体中文)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • Contact sales
  • Trial software

MathWorks

Accelerating the pace of engineering and science

MathWorks is the leading developer of mathematical computing software for engineers and scientists.

Discover…

Explore Products

  • MATLAB
  • Simulink
  • Student Software
  • Hardware Support
  • File Exchange

Try or Buy

  • Downloads
  • Trial Software
  • Contact Sales
  • Pricing and Licensing
  • How to Buy

Learn to Use

  • Documentation
  • Tutorials
  • Examples
  • Videos and Webinars
  • Training

Get Support

  • Installation Help
  • MATLAB Answers
  • Consulting
  • License Center
  • Contact Support

About MathWorks

  • Careers
  • Newsroom
  • Social Mission
  • Customer Stories
  • About MathWorks
  • Select a Web Site United States
  • Trust Center
  • Trademarks
  • Privacy Policy
  • Preventing Piracy
  • Application Status

© 1994-2022 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Instagram
  • YouTube
  • LinkedIn
  • RSS

Join the conversation