MATLAB For Dummies
Book image
Explore Book Buy On Amazon

To make data useful, you need to be able to open the MATLAB files containing it. Otherwise, there isn’t any point in saving the data. Likewise, not all your colleagues will have a copy of MATLAB, or they may want to use a different application to interact with the MATLAB data.

For you to use their data, you must be able to import data files created by other applications. When you want to share your data with others, you must export your data to files that are understood by other applications. MATLAB provides great support for both imported and exported data.

Opening MATLAB files

The fastest way to open any MATLAB file is to double-click its entry in the folder found in the Current Folder window. You can also right-click the entry and choose Open from the context menu. MATLAB automatically opens the file using a default application or method.

It’s important to realize that MATLAB always uses a default application or method. Data files are sometimes associated with other applications. In addition, some data files can be opened in more than one way.

When you want to use an alternative method of opening a file, you must rely on the underlying platform.

For example, when working with Windows, right-click the file and choose Show in Explorer from the context menu. A copy of Windows Explorer opens, and you can work with alternative applications in that copy. Right-click the file in Windows Explorer and choose one of the alternative applications shown in the Open With menu of the context menu.

image0.jpg

MATLAB also uses different techniques for interacting with files when you work with commands. The default action for a .mat file is to load it into MATLAB, not open it. However, you can either load it or open it as needed. Here are the two commands you use (assuming that you want to work with FirstWorkspace.mat):

  • open('FirstWorkspace.mat')

  • load('FirstWorkspace.mat')

The first command actually opens the workspace so that you can see a result in the Command window. However, the results aren’t loaded into the Workspace window as they normally would be if you double-clicked the file. To achieve this same effect, you must use the second command, which loads the workspace into MATLAB.

Importing data to MATLAB

MATLAB makes importing whatever data you need from an external source easy. The following steps show you how:

  1. Click Import Data in the Variable group of the Home tab.

    You see the Import Data dialog box. Notice that MATLAB defaults to showing every file it can import.

    If you find that the list of files is too long, you can click the Recognized Data Files drop-down list and choose just one of the common file types. The list displays just those files, making a selection easier.

    image1.jpg
  2. Highlight the file you want to import and click Open.

    MATLAB displays an Import dialog box that contains import information about the file. This dialog box contains settings that you use to import the data and ensure that it’s useful in MATLAB. Here, you see the settings for a comma-separated value (CSV) file, and the rest of the procedure assumes that you’re working with such a file. However, the process is similar for other file types.

    image2.jpg
  3. (Optional) Modify the settings as needed so that the data appears as it should appear in the Workspace window.

    You can choose to limit the amount of data imported by changing the range. It’s also possible to select a different delimiter (which changes how the data appears onscreen).

  4. Verify that the Unimportable Cells group has no entries.

    Cells that MATLAB can’t import might reflect an error or simply mean that you have some settings wrong.

  5. Click Import Selection.

    MATLAB imports the data. As alternatives, you can also choose to generate a script or function based on the data, rather than actually import the data into the workspace.

  6. Close the Import window.

You can read about the data formats that MATLAB can import. This site also contains commands that you can use to import the files rather than relying on the GUI to do the work. However, the GUI is always faster and easier to use, so it’s the recommended course.

Exporting data from MATLAB

You rely on commands in order to export data from MATLAB. The list of data formats includes commands in the Export column for each format that MATLAB supports.

Most of the commands work with a single variable. For example, if you want to export the information found in the ans variable to a CSV file, you type something like csvwrite('FirstWorkspace.csv',ans), where csvwrite() is the function, FirstWorkspace.csv is the name of the file, and ans is the name of the variable you want to export.

Along with csvwrite(), the most commonly used export commands are xlswrite(), which creates an Excel file, and dlmwrite(), which creates a delimited file. Both of these commands work much the same as csvwrite().

Some file formats require quite a bit of extra work. For example, to create an eXtensible Markup Language (XML) file, you must first build a document model for MATLAB to use.

About This Article

This article is from the book:

About the book authors:

John Paul Mueller is an author and technical editor with experience in application development, database management, machine learning, and deep learning. He has written hundreds of books and articles helping everyday people learn everything from networking to database management.

John Mueller has produced 114 books and more than 600 articles on topics ranging from functional programming techniques to working with Amazon Web Services (AWS). Luca Massaron, a Google Developer Expert (GDE),??interprets big data and transforms it into smart data through simple and effective data mining and machine learning techniques.

This article can be found in the category: