Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to extract data from complex text by MATLAB

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to extract data from complex texts by MATLAB. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

In fact, it can not be regarded as a complex text, it is still regular. This text contains the data collected by my laboratory equipment, as follows:

I lost part of the data, I need to extract the framed part of the data, this is a matrix, the column number on the top and the row number on the left are the serial numbers of the sensor.

A simple way to read data:

From the observation, we can see that the data we want to get is after the line at the beginning of CH#, and the line is followed by numeric values! So it's simple:

We use a while loop with the end condition of reaching the end of the file

In each loop, determine whether you have passed the line at the beginning of CH#.

If you haven't passed the line at the beginning of CH#, continue.

If you pass the line at the beginning of CH#, start reading.

Let's go directly to the code and give some simple instructions.

Clc

Clear

File opens dialog box, select file

[file, path] = uigetfile ({'* .TXT'}, 'Select Traveltime File')

% exit if no file is selected

If file = = 0

Return

End

% File full path

FullPath = [path, file]

% Open file

Fid = fopen (fullPath,'r')

%% matrix that saves the results

ElapsTimeData = []

% Flag start mode is'^ CH#', start flag has an initial value of 0

StartParten ='^ CH#'

StartFlag = 0

Read the file in a loop

While (~ feof (fid))

Read one line at a time and remove the whitespace on both sides

CurLine = strip (fgetl (fid))

If the start flag has been set to 1 and it is not a blank line, read the

If startFlag = = 1 & & ~ isempty (curLine)

Cur_data = cellfun (@ str2double, regexpi (strip (curLine),'\ s subscription, 'split'))

ElapsTimeData = [ElapsTimeData; cur_data]

End

When startParten is encountered, the next line begins with travel time data.

If regexpi (curLine, startParten)

StartFlag = 1

End

End

Delete the first column, which contains the sensor serial number

ElapsTimeData (:, 1) = []

About the regexpi function, it takes three parameters:

The first is the original string

The second is the pattern we want to match.

The third parameter is the regular expression output rule.

The bold boldface sentence in the above program

Regexpi (strip (curLine),'\ s subscription, 'split')

The source string is the current line

Matching pattern:\ s + represents one or more spaces (white space)

Output rules: I use split, which means: return all strings except matching strings!

So, I'm going to output all the characters except whitespace, and this matching doesn't start until we match to'^ CH#', so we can get the numbers in all the lines that follow the line of'^ CH#'.

If the third parameter uses match, you can write something like this, but the matching pattern is a little too much!

Regexpi (strip (curLine),'\ d +\.\ dbath, 'match')

In addition, regexpi returns an array of cells, and each matching result is placed in a single cell. So the cellfun I use here converts the characters in each cell into numeric values:

Cur_data = cellfun (@ str2double, regexpi (strip (curLine),'\ s subscription, 'split'))

Note:

In the code given above, I deleted the code dealing with errors, such as files that do not comply with the above rules, or we accidentally deleted some data from the parts to be extracted, and so on.

These can be handled with the try-catch structure, and I won't say much about it. Those who are interested can try it for themselves. In fact, when extracting data, it's best to add the code that handles error situations.

Read result:

Also code out some of the data.

The above is all the content of the article "how to extract data from complex text by MATLAB". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report