Updated June 17, 2023
Introduction to dlmread in Matlab
The ‘dlm’ command is used in Matlab to read ASCII numeric data. We can create the text files using this command and read the text files 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. Using the dlmwrite command, we can also modify existing text file content. This command improves the adaptability of the Matlab language by accessing data in other formats.
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, we first need to create that file with some data. There are various ways to write and read text files. We can write these files by passing parameter lists or directly adding data. We can also modify the existing text file by applying some operations on data.
Steps to read Excel files in Matlab:
- Clear workspace.
- Declare and assign data.
- Create a text file by using the ‘dlmwrite’ syntax ( dlmwrite (filename, [ data ] ).
- Declare a variable to read the file (optional).
- Use the dlmread command by using syntax. ( dlmread (filename ) ).
Examples of dlmread in Matlab
Given below are the examples of dlmread in Matlab:
Let’s take this example where our text file is ‘file1.txt’, and we’re adding this data to the file: [4 3 6 8 6]. So it will create one text file in the current directory. Here we use a simple method to create the file and then use the dlmread command to read text file1.
clear all ;
dlmwrite ( ' file1.txt ' , [ 4 3 6 8 6 ] )
dlmread ( ' file1.txt ' )
In this example, input data is declared using another variable, ‘data’ (data = [ 3 4 56 3 ; 32 54 67 43 ; 21 4 8 0 ] ). Data is written in ‘file2’.
clear all ;
data = [3 4 56 3 ; 32 54 67 43 ; 21 4 8 0] dlmwrite ( ' file2.txt ' , [ data ] )
dlmread ( ' file2.txt ' )
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 a text file ‘file2’ and read the same.
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 ')
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 the dlm read function. This example shows that we can perform operations on existing data and read the same.
data=[3 3 ;32 54 ; 21 4 ] dlmwrite('file4.txt',[data *5 data /5])
To deal with text files in Matlab is a little difficult. But by using Matlab, we can easily import and export the data from the text file to Matlab or Matlab to a text file. We can read the text file data in various ways per our needs and application needs. Matlab operates on text files very effectively and efficiently.
This has been a guide to dlmread in Matlab. Here we discuss the introduction; how does dlmread work in Matlab? Along with examples and output, respectively. You may also have a look at the following articles to learn more –