Updated March 4, 2023
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.
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) |
|
[P,Q,R,…] = textread(data,format,N) |
|
[…] = textread(…,param,value,…) |
|
Attributes:
Given below are the attributes:
Attribute | Possible value | Description |
Bufsize |
|
|
commentstyle |
|
|
Delimiter |
|
|
emptyvalue |
|
|
endofline |
|
|
expchars |
|
|
headerlines |
|
|
whitespace |
|
|
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.
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.
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.
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.
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 –