EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial dlmread in Matlab
 

dlmread in Matlab

Priya Pedamkar
Article byPriya Pedamkar

Updated June 17, 2023

dlmread in Matlab

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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, 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:

  1. Clear workspace.
  2. Declare and assign data.
  3. Create a text file by using the ‘dlmwrite’ syntax ( dlmwrite (filename, [ data ] ).
  4. Declare a variable to read the file (optional).
  5. Use the dlmread command by using syntax. ( dlmread (filename ) ).

Examples of dlmread in Matlab

Given below are the examples of dlmread in Matlab:

Example #1

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.

Code:

clear all ;
dlmwrite ( ' file1.txt ' , [ 4 3 6 8 6 ] )
dlmread ( ' file1.txt ' )

Output:

dlmread command

Example #2

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’.

Code:

clear all ;
data = [3 4 56 3 ; 32 54 67 43 ; 21 4 8 0] dlmwrite ( ' file2.txt ' , [ data ] )
dlmread ( ' file2.txt ' )

Output:

dlmread matlab 2

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 a 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: 

two variables

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 the dlm read function. This example shows that we can perform operations on existing data and 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:

read function

Conclusion

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.

Recommended Articles

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 –

  1. Matlab Inverse Function
  2. Find Function Matlab
  3. Matlab Logical Operators
  4. Sparse Matrix in MATLAB
  5. Working of Reshape Function in Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW