开发者

parse text with Matlab

I have a text file (output from an old program) that I'd like to clean. Here's an example of the file contents.

*|V|0|0|0|t|0|1|1|4|11|T4|H01|||||||||||||||||||||| 


P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0|||||||||||||||| 
*|A1|A1|A7|A16|F|F|F|F|F|F|F||||||||||||||||||||||| 
*|||||kV|kV|kV|MW|MVAR|S|S|||||||||||||||||||||||| 
N|I|01|H01N01|H01N01|132|125.4|138.6|0|0||||||||||||||||||||| 
N|I|01|H01N02|H01N02|20|19|21|0|0||||||||||||||||||||||| 
N|I|01|H01N03|H01N03|20|19|21|0.42318823|0.204959433||||||||||||||||||||| 
||||||||||||||||| 
||||||||||||||||| 
L|I|H010203|H01N02|H01N03|1.884|360|0.41071|0.207886957||3.19E-08|3.19E-08||||||||||| 
L|I|H010304|H01N03|H01N04|1.62|360|0.35316|0.1787563||3.19E-08||3.19E-08|||||||||||| 
L|I|H010405|H01N04|H01N05|0.532|360|0.11598|0.058702686||3.19E-08||3.19E-08||||||||||| 
L|I|H010506|H01N05|H01N06|1.284|360|0.27991|0.14168092||3.19E-08||3.19E-08|||||||||||| 
S|SH01|SEZIONE01|1|-3|+3|-100|+100||||||||||||||||||| 
S|SH02|SEZIONE02|1|-3|+3|-100|+100||||||||||||||||||| 
S|SH03|SEZIONE03|1|-3|+3|-100|+100||||||||||||||||||| 
||||||||||||asasasas 
S|SH04|SEZIONE04|1|-3|+3|-100|+100||||||||||||||||||| 
*|comment 
S|SH05|SEZIONE05|1|-3|+3|-100|+100|||||||||||||||||||

I'd li开发者_如何转开发ke it to look like:

*|V|0|0|0|t|0|1|1|4|11|T4|H01|||||||||||||||||||||| 
*|comment 
*|comment 
P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0|||||||||||||||| 
*|A1|A1|A7|A16|F|F|F|F|F|F|F||||||||||||||||||||||| 
*|||||kV|kV|kV|MW|MVAR|S|S|||||||||||||||||||||||| 
N|I|01|H01N01|H01N01|132|125.4|138.6|0|0||||||||||||||||||||| 
N|I|01|H01N02|H01N02|20|19|21|0|0||||||||||||||||||||||| 
N|I|01|H01N03|H01N03|20|19|21|0.42318823|0.204959433||||||||||||||||||||| 
*|comment|||||||||||||||| 
*|comment||||||||||||||||| 
L|I|H010203|H01N02|H01N03|1.884|360|0.41071|0.207886957||3.19E-08||3.19E-08||||||||||| 
L|I|H010304|H01N03|H01N04|1.62|360|0.35316|0.1787563||3.19E-08||3.19E-08|||||||||||||| 
L|I|H010405|H01N04|H01N05|0.532|360|0.11598|0.058702686||3.19E-08||3.19E-08||||||||||| 
L|I|H010506|H01N05|H01N06|1.284|360|0.27991|0.14168092||3.19E-08||3.19E-08|||||||||||| 
*|comment 
*|comment 
S|SH01|SEZIONE01|1|-3|+3|-100|+100||||||||||||||||||| 
S|SH02|SEZIONE02|1|-3|+3|-100|+100||||||||||||||||||| 
S|SH03|SEZIONE03|1|-3|+3|-100|+100||||||||||||||||||| 
S|SH04|SEZIONE04|1|-3|+3|-100|+100||||||||||||||||||| 
S|SH05|SEZIONE05|1|-3|+3|-100|+100||||||||||||||||||| 

The data are divided into 'packages' distinct from the first letter (PNLS). Each package must have at least two dedicated lines (* |) which is then read as a comment. The white lines between different letters are filled with character * |. The lines between various letters that do not begin with * | to be added. The white lines and characters 'random' between identical letters are removed.

Perhaps it is clearer in the example files.

How do I manipulate the text? Thank you in advance for the help.


Use fileread to get your file into MATLAB.

text = fileread('my file to clean.txt');

Split the resulting character string up by splitting on the new lines. (The newlines characters depend on your operating system.)

lines = regexp(text, '\r\n', 'split');

It isn't entirely clear exactly how you want the file cleaned, but these things might get you started.

% Replace blank lines with comment string
blanks = cellfun(@isempty, lines);
comment = '*|comment';
lines(blanks) = cellstr(repmat(comment, sum(blanks), 1))

% Prepend comment string to lines that start with a pipe
lines = regexprep(lines, '^\|', '\*\|comment\|')

You'll be needing to know your way around regular expressions. There's a good guide to them at regular-expressions.info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜