## Import Numeric Data from Text Files into Matrix

Import numeric data as MATLAB® arrays from files stored as comma-separated or delimited text files.

### Import Comma-Separated Data

This example shows how to import comma-separated numeric data from a text file. Create a sample file, read all the data in the file, and then read only a subset starting from a specified location.

Create a sample file named `ph.dat` that contains comma-separated data and display the contents of the file.

```A = 0.9*gallery('integerdata',99,[3 4],1); writematrix(A,'ph.dat','Delimiter',',') type('ph.dat')```
```85.5,54,74.7,34.2 63,75.6,46.8,80.1 85.5,39.6,2.7,38.7 ```

Read the file using the `readmatrix` function. The function returns a `3`-by-`4` `double` array containing the data from the file.

`M = readmatrix('ph.dat')`
```M = 3×4 85.5000 54.0000 74.7000 34.2000 63.0000 75.6000 46.8000 80.1000 85.5000 39.6000 2.7000 38.7000 ```

Import only the rectangular portion of data starting from the first row and third column in the file. Create an import options object and specify the columns and rows to import using the `SelectedVariableNames` and `DataLines` properties. Then, import the selected portion of the data from the file.

```opts = detectImportOptions('ph.dat'); opts.SelectedVariableNames = {'Var3','Var4'}; opts.DataLines = [1 3]; readmatrix('ph.dat',opts)```
```ans = 3×2 74.7000 34.2000 46.8000 80.1000 2.7000 38.7000 ```

### Import Delimited Numeric Data

This example shows how to import numeric data delimited by any single character using the `writematrix` function. Create a sample file, read the entire file, and then read a subset of the file starting at the specified location.

Create a tab-delimited file named `num.txt` that contains a `4`-by-`4` numeric array and display the contents of the file.

```A = gallery('integerdata',99,[4,4],0); writematrix(A,'num.txt','Delimiter','\t') type('num.txt')```
```95 89 82 92 23 76 45 74 61 46 61 18 49 2 79 41 ```

Read the entire file. The `readmatrix` function determines the delimiter automatically and returns a 4-by-4 `double` array.

`M = readmatrix('num.txt')`
```M = 4×4 95 89 82 92 23 76 45 74 61 46 61 18 49 2 79 41 ```

Read only the rectangular block of data beginning from the second row, third column, in the file. Create an import options object and specify the columns and rows to import using the `SelectedVariableNames` and `DataLines` properties. Then, import the selected portion of the data from the file.

```opts = detectImportOptions('num.txt'); opts.SelectedVariableNames = {'Var3','Var4'}; opts.DataLines = [2 4]; readmatrix('num.txt',opts)```
```ans = 3×2 45 74 61 18 79 41 ```