Importing Data from Text Files Interactively
You can interactively import delimited and fixed width text files with the Import tool. We will look at importing numeric and text data, handling missing data, and generating MATLAB code. Let's say that you wanted to import this file, electricity.txt.
First, let's open it up outside of MATLAB to get a better look. The file holds information about electricity usage. It is tab delimited and contains several data types-- text such as Name, numbers such as ID, monetary amounts denoted with a dollar sign such as residential revenue, and dates. You can open the Import tool using the Import Data icon in the Home tab of the tool strip, and then choose the file you wish to import.
The Import tool that appears provides a view into the text file. For large files, only a beginning subset of the data is loaded in for this preview. Therefore, you can create a custom import to load in part of a large data file without having to use the memory required to load in the entire data file. When choosing initial settings of how to format your data for import, the tool makes educated choices based on file structure and content.
The tool has correctly identified this file as being tab delimited. It chooses variable names based on the structure of the file and chooses variable types. See that the tool has chosen number for ID and text for Name. These initial settings. However, can be adjusted. If you wish to use a different delimiter or even a combination of delimiters, you can choose that or even define a custom delimiter in the pull-down menu.
You can also modify the variable type. For instance, let's say you wanted to bring in the date as a date-time instead of text. The variable type is now date-time, and if you hover over a cell, you can see how the data will be imported.
Since the values for revenue are numbers with a constant character in front-- a dollar sign-- the tool strips off any constant characters and represents the value as a number. This would be true of any column whose majority of data was a number with a constant prefix or suffix. If you wanted to bring it in as text, you can choose that option instead.
In this case, a period is used for the decimal separator. However, if you're given a text file that uses commas instead, you can set that as an option. If you are in a country where decimal separators are usually commas, the comma will be your default value.
By default, the data will be imported as column vectors. However, you can choose to bring it in as a numeric matrix. The yellow highlights indicate cells which will not be imported in that case. Text is replaced with NaNs since numeric matrices can't hold text. You can also bring in the data as a cell array or as a table.
You can adjust the columns and rows of the data you wish to bring in either by highlighting the sections or by directly entering the range. You can also choose previous ranges used. The initial range chosen for this file is marked with initial. Once you've established the way you wish to import the data, you can either import it directly into the MATLAB workspace or you can generate a script or a function. You can generate code from the Import tool to make it easier to repeat the data import.
This time, let's generate a script. We can save the file and run it from the editor. You can import data similarly from fixed width text files. Sodium.txt contains data related to patients' sodium levels measured over the course of a drug trial. This time, let's bring in the file by right clicking the file in the current folder window.
Note that in the Import tool, each document is in a separate tab, and multiple can be opened simultaneously, just like editor files. This file is correctly identified as being fixed width, and the tool has made an initial estimate of where the boundaries are. You can adjust these boundaries or insert additional fixed width boundaries.
You can split columns and merge them. Empty cells are automatically replaced with NaNs, but this is adjustable. Since this is a fixed width text file, the column lists are not adjustable as with delimited files. Items temporarily expand when you edit them, giving you full view.
This time, let's import our data as a table and generate a function. The comments at the top of the function include example syntax and how to call the generated function. Save the function and you can call from the command line. That concludes the video overview of the interactive capabilities for text file import. Try this in MATLAB now or watch one of the other videos.
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.