MathWorks - Mobile View
  • Sign In to Your MathWorks AccountSe connecter
  • Access your MathWorks Account
    • Mon compte
    • Mon profil
    • Mes licences
    • Se déconnecter
  • Produits
  • Solutions
  • Le monde académique
  • Support
  • Communauté
  • Événements
  • Obtenir MATLAB
MathWorks
  • Produits
  • Solutions
  • Le monde académique
  • Support
  • Communauté
  • Événements
  • Obtenir MATLAB
  • Sign In to Your MathWorks AccountSe connecter
  • Access your MathWorks Account
    • Mon compte
    • Mon profil
    • Mes licences
    • Se déconnecter

Vidéos et webinars

  • MathWorks
  • Vidéos
  • Vidéos
  • Recherche
  • Vidéos
  • Recherche
  • Contacter un commercial
  • Version d'essai
  Register to watch video
  • Description
  • Full Transcript
  • Related Resources

How to Import Data from Files Programmatically

Elsie Eigerman, MathWorks

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

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
3:04
How to Change Properties in GUIDE from a Button Press
2:09
How to Store a Series of Vectors from a for Loop
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: .

Select web site

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

  • Contacter un commercial
  • Version d'essai

Découvrir les produits

  • MATLAB
  • Simulink
  • Version étudiante
  • Support Hardware
  • File Exchange

Essayer ou Acheter

  • Téléchargements
  • Version d'essai
  • Contacter un commercial
  • Tarifs et licences
  • Comment acheter

Se Former

  • Documentation
  • Tutoriels
  • Exemples
  • Vidéos et webinars
  • Formation

Obtenir de l'aide

  • Aide à l'installation
  • Forum MATLAB
  • Services de consulting
  • Gestion Licences
  • Contacter le support technique

La société

  • Offres d'emploi
  • Actualités
  • Social Mission
  • Contacter un commercial
  • La société

MathWorks

Accelerating the pace of engineering and science

MathWorks est le leader mondial des logiciels de calcul mathématique pour les ingénieurs et les scientifiques.

Découvrir…

  • Select a Web Site United States
  • Brevets
  • Marques déposées
  • Charte de confidentialité
  • Lutte anti-piratage
  • État des applications

© 1994-2021 The MathWorks, Inc.

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

Rejoignez la conversation

This website uses cookies to improve your user experience, personalize content and ads, and analyze website traffic.  By continuing to use this website, you consent to our use of cookies.  Please see our Privacy Policy to learn more about cookies and how to change your settings.