Read text files and extract values

Source: Internet
Author: User

Xxx_rdw.textread('xxx.txt ',' % s', 'delimiter', '/N ');

Is to read the file xxx.txt in simplified format. The xxx_rd is an array of N * 1 cells. The content of each cell is a string corresponding to a line of text.

Delimiter is a delimiter. You can specify multiple delimiter. For example, you can specify '/N' and' at the same time. 'And space are separators:

Xxx_rdw.textread('xxx.txt ',' % s', 'delimiter', '/N ,');

Note: For clarity, the names of variables are usually received.CompositionIf the file name is xxx.txt, the variable name xxx_rd is received, and xxx_rd is processed in a series to get the final variable XXX.

--

The advantage of reading files in the above method is that there is no omission in the information-first read the text unharmed, and then perform various processing, and the processing should also be performed on the copy, ensure the integrity of the source.

The most common processing is to extract numeric values. For a line of text (string), values are extracted sequentially and stored in a vector, which can be implemented by the following custom functions:

Function valus = getvalu (STR)
Valus = [];
Valustr = [];
For I = 1: length (STR)
If (STR (I)> = '0' & STR (I) <= '9') | STR (I) = '.'
Valustr = [valustr, STR (I)];
Else
If ~ (Isempty (valustr) | (length (valustr) = 1 & valustr (1) = '.'))
Valus = [valus, str2num (valustr)];
Valustr = [];
End
End
End
If ~ (Isempty (valustr) | (length (valustr) = 1 & valustr (1) = '.'))
Valus = [valus, str2num (valustr)];
End
End

If you want to form a matrix of values extracted from each row, you only need to write a loop.

--

In addition, the common processing is to fill in a struct with the content of the read text (which also needs to extract numeric values). It is also done through loops and is not difficult to write.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.