EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • 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
  • Login
Home Data Science Data Science Tutorials Matlab Tutorial Matlab textread

Matlab textread

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

Matlab textread

Introduction to Matlab textread

Matlab textread() function is designed to read data from a text file or from any data set, and results out multiple outputs. It is a function that handles various type of the problems or tasks with respect to the file input for a user. In the function call for textread(), output variable is of array type that receives the contents of the file that is defined in the datatype of a string. A single value can be read from a file user can modify using textread() by passing another parameter giving number of values to read.

ADVERTISEMENT
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax of Matlab textread

The format of the input, specified in the form of either a string scalar or character vector, determines the types and number of return arguments. A subset of the conversion specifiers as well as that of C language fscanf() routine conventions are supported by this format. The total number of return arguments equals to the number of arguments indicated in the contents of format.

Syntax Description
[P,Q,R,…] = textread(data,format)
  • This syntax for implementation of textread(filename,format) is to read data from the file filename or dataset ‘data’, into the variables P, Q, R, and so on, following the specified format, until reading operation is not completed for the entire file.
  • The filename and format attributes are specified as character vectors or string scalars.
[P,Q,R,…] = textread(data,format,N)
  • This syntax for implementation of textread(filename,format) is to read data from the file filename or dataset ‘data’, into the variables P, Q, R, and so on, following the specified format, reusing the format for N times, where N can be defined is a positive integer.
  • In case of N is defined as negative integer, the function executes until reading operation is not completed for the entire file.
[…] = textread(…,param,value,…)
  • This syntax for implementation of textread(filename,format) is to read data from the file filename or dataset ‘data’, into the variables P, Q, R, and so on, following the specified format whereas customization for textread() can be applied using param-value pairs

Attributes:

Given below are the attributes:

Attribute Possible value Description
Bufsize
  • A Positive integer.
  • Default is 4095.
  • It defines the maximum length of the character vector, in bytes.
commentstyle
  • Matlab.
  • Shell.
  • C.
  • C++.
  • Characters after % are ignored.
  •  Characters after # are ignored.
  • Characters between /* and */ are ignored.
  • Characters after // are ignored.
Delimiter
  • One or more characters.
  • Default is none.
  • Act as delimiters between elements.
emptyvalue
  • Scalar double.
  • Default is 0.
  • Value assigned to empty cells while reading delimited files.
endofline
  • Single character or ‘\r\n’.
  • Default is determined from file.
  • Character that denotes the end of a line.
expchars
  • Exponent characters
  • Default value is eEdD.
  • Used for power function.
headerlines
  • Positive integer.
  • To ignore the specified number of lines at the beginning of the input file.
whitespace
  • ‘ ‘- Space
  • \b- Backspace
  • \n- Newline
  • \r- Carriage return
  • \t-Horizontal tab
  • Default is ‘ \b\t’.
  • Vector of characters is treated as white space.

Various values that are supported as value for format such as:

  • %d: Reading a signed integer value.
  • %c: Reading characters, including white space.
  • %u: Reading an integer value.
  • %s: Read a white-space or delimiter-separated text.
Note: In format, White-space characters are ignored.

Examples of Matlab textread

Given below are the examples mentioned :

Example #1

Reading operation on a free format file using % for all fields.

Considering the first line in the user defined file myfile.dat is:

Code:

NameStrstring1 22.34 46

Reading operation for the first line of the file is called by using textread() as follow:

Code:

[names, first, x, y] = textread(myfile.dat',’%s %s %f %d', 1)

Output:

The names and first element is presented with string format, value of x is presented as floating point number and value of y is presented as an integer.

Matlab textread

Example #2

Reading operation on a fixed format file using %, the floating point value being ignored.

Considering the first line in the user defined file myfile.dat is:

Code:

NameStr  string1 22.34 46

Reading operation for the first line of the file is called by using textread() as follow:

Code:

[names, first, y] = textread(myfile.dat','%c %s %*f %d', 1)

Output:

The names and first element is presented with string format and value of y is presented as an integer. Value of x which is defined with the format of floating point number is ignored.

Using %

Example #3

Reading operation on a free format file using %, having matching characters ignored by means of literal.

Considering the first line in the user defined file myfile.dat is:

Code:

NameStr    string1 22.34 46

Reading operation for the first line of the file is called by using textread() as follow:

Code:

[names, strval, x, y] = textread(myfile.dat','%s string%d %f %d', 1) 

Output:

The names element is presented with string format and value of y is presented as an integer. Value of ‘strval’ which is defined with the format of finding matching character vector is ignored.

Matlab textread 3

Example #4

Reading 100 values in a loop using textread().

Code:

textread('file_str', '%d', 100); ); %reads 100 values from  given file at once
%Beginning of loop
for i=1:100
% This reads a single integer from file_str.
textread('file_str, '%d', 1); %reads 100 values from  given file one by one
end %End of the loop

Additional Note:

  • textread() is useful for reading operation on text files following a defined format. textread() handles both fixed as well as free format files.
  • When textread() reads a consecutive series of whitespace values, it treats series of whitespaces as one white space. Similarly, when it reaches a consecutive series of delimiter values, it treats each element separately as an individual delimiter.
  • In order to preserve leading and trailing spaces in the text, the whitespace parameter is used as shown below:

textread(‘mydata.txt’, ‘%s’, ‘whitespace’, ”)

ans =

‘   White space      white      space             white           space      ‘

  • textread() function is obsolete in the current Matlab versions. This method is now replaced with textscan().
  • Implementation of textread() method attempts to read complete information that it can, from a file. It can be considered as “one shot gets all” type of function.

Recommended Articles

This is a guide to Matlab textread. Here we discuss the introduction to Matlab textread along with appropriate syntax, attributes and respective examples. You may also have a look at the following articles to learn more –

  1. MATLAB Unique
  2. Matlab Plot Legend
  3. MATLAB Normalize
  4. Low Pass Filter Matlab
ADVERTISEMENT
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW