‘dlm’ command is used in Matlab to read ASCII numeric data. We can create the text files by using this command as well as we can read the text files by using this command. There are two operations in Matlab one is to create test files and the other is to read or open text files. These commands are dlmread and dlmwrite. dlmread command is used to read or open the existing text files in Matlab. dlmwrite command is used to write or create text files in Matlab. By using dlmwrite command we can also modify the content of existing text files. This command improves the adaptability of the Matlab language by accessing data in other formats.
Syntax:
dlmread (filename)
dlmread (filename, parameters list)
dlmread (filename, [data])
dlmread (filename, [operations on data])
How does dlmread work in Matlab?
To read any text file in Matlab first we need to create that file with some data into it. There are various ways to write and read text files. These files can be written by passing parameter lists, direct data. We can also modify the existing text file by applying some operations on data.
Steps to read excel file in Matlab:
- Clear workspace.
- Declare and assign data.
- Create a text file by using ‘dlmwrite’ syntax ( dlmwrite (filename, [ data ] ).
- Declare variable to read file (optional).
- Use dlmread command by using syntax. ( dlmread (filename ) ).
Examples of dlmread in Matlab
Given below are the examples of dlmread in Matlab:
Example 1:
In this example, the text file is ‘ file1.txt ’ and data added to the file is [ 4 3 6 8 6 ]. So it will create one text file in the current directory. Here we are using a simple method to create the file and then we use dlmread command to read text file1.
Code:
clear all ;
dlmwrite ( ' file1.txt ' , [ 4 3 6 8 6 ] )
dlmread ( ' file1.txt ' )
Output:

Example 2:
In this example input data is declared by using another variable, which is ‘ data ’ (data = [ 3 4 56 3 ; 32 54 67 43 ; 21 4 8 0 ] ) . Data is written in ‘file2’.
Code:
clear all ;
data = [3 4 56 3 ; 32 54 67 43 ; 21 4 8 0] dlmwrite ( ' file2.txt ' , [ data ] )
dlmread ( ' file2.txt ' )
Output:

Example 3
In this example input data is declared by using another two variables, which is ‘data’ (data1= [3 4 56 3 ; 32 54 67 43 ; 21 4 8 0] and data2 = [3 4 56 7 ; 3 46 7 2 ; 2 3 4 1] Data is written in text file ‘file2’ and read the same.
Code:
clear all ;
data1 = [3 4 56 3 ; 32 54 67 43 ; 21 4 8 0] data2 = [3 4 56 7 ; 3 46 7 2 ; 2 3 4 1] dlmwrite ( ' file3.txt ' , [data1 ; data2 ] )
dlmread (' file3.txt ')
Output:

Example 4:
In this example, input data is declared by using another one variable, which is ‘data’ (data=[3 3;32 54; 21 4 ]). Data is written in the text file ‘file4’. To read the file we use dlm read function. This example shows that we can perform operations on existing data and we can read the same.
Code:
clear all;
data=[3 3 ;32 54 ; 21 4 ] dlmwrite('file4.txt',[data *5 data /5])
a=dlmread('file4.txt')
Output:
