Working with Data Files
A typical ASCII data file has one or more lines of text at the beginning. These might be comments that describe what the data represents, the date it was created, and who created the data, for example. These lines are called the header. One or more lines of data, arranged in rows and columns, follow the header. The numbers
in each row might be separated by spaces or by commas. If it is inconvenient to edit the data file, the MATLAB environment provides many ways to bring data created by other applications into the MATLAB
workspace, a process called importing data, and to package workspace variables so that they can be used by other applications, a process called exporting data. Your choice of which mechanism to use depends on which operation you are performing, importing or exporting, and whether you are working with ASCII data or binary data. To make importing data easier, both ASCII and binary, MATLAB includes a graphical user interface, called the Import Wizard, that leads you through the import process

Importing Data from Externally

Generated Files You can enter data into MATLAB by typing it into an array or you can enter and store the data in an M-file. However, either method is inconvenient when you have a lot of data. Such data is often generated by some other application. For example, you might be given a data file generated by a spreadsheet program, or

you might have to analyze data collected by a laboratory  instrumentation system. Many applications support the ASCII file format, so you are likely to receive a data file stored in this form,at. If the file has a header or the data is separated by commas, MATLAB will produce an error message. To correct this situation, first load the data file into a text editor, remove the header, and replace the commas with spaces (the number of spaces does not matter as long as there is at least one).To retrieve this data into MATLAB, type load f i 1 name. If the m-file has m lines with n values in each line, the data will be assigned to an m x ~ matrix having the same name as the file with the extension stripped off. For example, if your data file contains 10 lines and 3 columns of data and is named force. data typing load force. data creates the 10 x 3 matrix force, which you can use in your MATLAB session just as you would use any other variable. Your data file can have any extension except. mat, so that MATLAB will not try to load the file as a work-space file.

Importing Spreadsheet Files
Some spreadsheet programs store data in the . wk1 format. You can use the command M= wk1 read ( , f i 1 name ‘) to import this data into MATLAB and store it in the matrix M.

The command A =xlsread{‘ file name’) imports the Microsoft Excel workbook file filename. xls into the array A.The command [A, 13]= xlsread ( ‘file name’ ) imports all numeric data into the array A and all text

data into the cell array B.

The. Import Wizard
To import ASCII data, you must know how the ‘data in the file is formatted. For example, many ASCII data files use a fixed (or uniform) format of rows and columns.For these files, you should know the following.

• How many data items are in each row?
• Are the data items numeric, text strings, or a mixture of both types?
• Does each row or column have a descriptive text header?
• What character is used as~ delimiter, that is, the character used to
.separate the data items in each row? The delimiter is also called the
column separator To find out how your ASCII data file is formatted, view it in a text editor.The data format will usually fall into one of the following categories

  1.  Space-delimited ASCII data files,
  2. Mixed text and numeric ASCII data files,
  3. . ASCII data files with text headers (the only text is at the head of each data column), or
  4.  ASCII data files with nonspace delimiters (usually semicolons).

You can use the Import Wizard to import many types of ASCII data formats,including data on the clipboard. Note that when you use the Import Wizard to create a variable in the MATLAB workspace, it overwrites any existing variable in the workspace with the same name without issuing a warning. The Import Wizard presents a series of dialog boxes in which you:

  1. Specify the name of the file you want to import,
  2. . Specify the delimiter used in the file, and

Select the variables that you want to import

The following provides a step-by-step procedure for using the Import Wizard to import this sample tab-delimited, ASCII data file, test data. txt.


  1. Activate the Import Wizard by selecting the Import Data option on the MATLAB Desktop File menu. The Import Wizard displays a dialog box that asks you to specify the name of the file you want to import. You can enter the file name in the text entry field or click the browse button to find the file you want to import. When the Import Wizard opens the file, it displays a preview of the data in the file. You can use the preview to  verify that you have specified the correct file. To continue with the import process, click Next.
  2. The Import Wizard processes the contents of the file and displays tabs identifying the variables it recognizes in the file, and displays a portion of the data in a grid, similar to a spreadsheet (see Figure 3.4-1). The Import Wizard uses the space character as the default delimiter. If your file uses in other character as a delimiter, the Import Wizard attempts to identify the delimiter. Make sure the correct delimiter button is highlighted; if not, then
The first screen in the Import Wizard.

The first screen in the Import Wizard.

click on the correct one. In this example, the Import Wizard correctly , interprets the contents of the tab-delimited sample file. If the delimiter is not listed, check the Other button and type the character in the text field. Click Next to continue the import operation.

3. In the next dialog box, the Import Wizard displays a list of the variables it found in the file. It also displays the contents of the first variable in the list. To view the contents of a variable, click on its name. (The variable

displayed in the dialog box is highlighted in the list.) In this example there is only one variable, named testdata

4. Choose the variables you want to import by clicking the check box next to their names. By default, all variables are checked for import. After selecting the variables you want to import, click the Finish button to import the data into the MATLAB workspace. This button dismisses the Import Wizard.

To import data from the clipboard, select Paste Special from the Edit menu. The
” proceed with step 2. The default variable name is A-pastestpecial.

Importing ASCII Data Files with Text Headers

Follow the same procedure to use the Import Wizard to import an ASCII data file that contains text headers. You will see a tab for the numeric data, a tab for all the text in the file, and p tab for the text headers. For example, the following file, temperature data contains space-delimited numeric data with a one-line text header


The default name given to the text headers by the Import Wizard is colheaders, and that given to the numeric data is data. In the Import Wizard screen that asks , you to select variables to load.If you select “Create variables matching preview,” when you click Finish you will have the variables colheaders and data in the workspace. The variable data is a numeric array containing the temperatures, and the variable colheaders is a cell array containing the headers. On the other hand, if you select “Create vectors from each column using column names,” when you click Finish you will have the variables Temp1, Temp2, and Temp3 in the workspace. These are numeric arrays. The appropriate choice depends on what you want to do with the data .

Importing Binary Data Files
You can use the Import Wizard to import many types of binary data formats, including MAT-files in which you saved previous sessions: The procedure is the

same as for loading ASCII data files. You will see a text tab if your file contains text data, and a data tab if it contains numeric data.

Exporting Delimited ASCII Data Files
You might want to export a MATLAB matrix as an ASCII data file where the rows and columns are represented as space-delimited, numeric values. To export an array as a delimited ASCII data file, you can use either the save command, specifying the -ASCI I qualifier, or you can use the dlmwr i t e function. The save command is easy to use; however, the dlmwri te function provides more flexibility, allowing you to specify any character as a delimiter. and to export subsets of an array by specifying a range of values. Suppose you have created the array A = [1 2 3 4; 5 6 7 8] in MATLAB. To export the array using the save command, type the following in the Command window.

»save my_data.out A -ASCII

If you view the created file in a text looks like this

1.0000000e+OOO .2. OOOOOOOe+OOO 3-.0000000e+OOO 4.OODooooe+oo~
5.0000000e+OOO 6.0000000e+OOO 7.0000000e+OOO 8.0000000e+OOO

By default, save uses spaces as delimiters, but you can use tabs instead of spaces .
by specifying the -tab qualifier. To export an array in ASCII format and specify the delimiter used in the file, use the dlmwr i t e function. For example, to export the array A as an ASCII data file that uses semicolons as a delimiter, type the following

»dlmwrite (‘my_data. out’ ,A, ‘;’)

If you view the created file in a text editor, it looks like this:

1;2;3;4 5;6;7;8

Note that dlmwrite does not insert delimiters at the end of rows. By default, if you do not specify a delimiter, dlmwrite e uses commas as a delimiter. You can specify a space (‘ ,) as a delimiter or you can specify no delimiter by using a
pair of single quotes

Share This