Programming |
The easiest way to import data into MATLAB is to use the Import Wizard. You do not need to know the format of the data to use this tool. You simply specify the file that contains the data and the Import Wizard processes the file contents automatically.
This section discusses how to import these types of data using the wizard:
You can also use the Import Wizard to import HDF data. See Using the HDF Import Tool for more information.
Using the Import Wizard with Text Data
To import text data using the Import Wizard, perform these steps:
uiimport
function to start the Import Wizard.
Specifying the Delimiter
When the Import Wizard opens a text file, or copies data from the clipboard, it displays a portion of the raw data in the preview pane of the dialog box. You can use this display to verify that the file contains the data you expected.
The Import Wizard also attempts to process the data, identifying the delimiter used in the data. The Import Wizard displays the variables it has created based on its interpretation of the delimiter, using tabbed panels to display multiple variables.
For example, in the following figure, the Import Wizard has opened this sample file, grades.txt
:
In the figure, note how the Import Wizard has correctly identified the space character as the delimiter used in the file and has created three tabs from the data:
data
contains all the numeric data in the file.
textdata
contains all the text found in the file.
rowheaders
contains the names in the left-most column of data.
Handling Alphabetic Data. The Import Wizard recognizes data files that use row or column headers and extracts these headers into separate variables. It can also ignore any text header lines that might precede the data in a file.
Specifying Other Delimiters. If the Import Wizard cannot determine the delimiter used in the data, it displays a preview of the raw data, as before, but the variables it displays are not correct. If your data uses a character other than a comma, space, tab, or semicolon as a delimiter, you must specify it by clicking the Other button and entering the character in the text box. The Import Wizard immediately reprocesses the data, displaying the new variables it creates.
Selecting the Variables to Import
The Import Wizard displays a list of the variables it has created from your data. To select a variable to import, click in the check box next to its name. By default, all variables are selected.
The Import Wizard displays the contents of the variable that is highlighted in the list in the right pane of the dialog box. To view the contents of one of the other variables, click it. Choose the variables you want to import and click Next.
Changing the Variable Selection. By default, the Import Wizard puts all the numeric data in the file into one variable. If the file contains text data, the Import Wizard puts it in a separate variable. If the file contains row or column headers, the Import Wizard puts them in separate variables, called rowheaders
or colheaders
, respectively.
In some cases, it might be more convenient to create a vector from each row or column of data and use the row header or column header text as the name of each variable. To do this, click the appropriate button from the list of buttons at the top of the dialog box.
For example, it eases calculation of the student averages if you create a separate vector for each student that contains that student's grades. To create these variables, click the Create vectors from each row using row names button. When you click this option, the Import Wizard reprocesses the file, creating these new vectors.
When you are satisfied with the list of vectors to be imported, click Finish to bring the data into the MATLAB workspace. This button also dismisses the Import Wizard. The MATLAB workspace now contains the vectors.
Text Data | Using the Import Wizard with Binary Data |
© 1994-2005 The MathWorks, Inc.