function [ ORG, ARV, STA, PHA, EVD ] = cssm( filespec )

str = fileread( filespec );

xpr = '(?<=(^|\n))[ ]*201[4567].+?(?=($|[ ]*201[4567]))';

blocks = regexp( str, xpr, 'match' );

nnl = length( strfind( str, char(10) ) );

len = length( blocks );

ORG = nan(len,14);

N = nnl - len + 1;

STA = repmat( '-', [N,5] );

ARV = nan(N,1);

PHA = repmat( '-', [N,1] );

EVD = nan(N,1);

nextORG = 1;

nextSTA = 1;

for cac = blocks

S0 = regexp( cac{1}, '\n', 'split', 'once' );

S1 = textscan( S0{1}, '%f%f%f%f%f%f%f%f%f%f%f%f%f%f' ...

, 'CollectOutput',true );

ORG( nextORG, : ) = S1{1};

MATDAY_ARV = datenum( S1{1}(1:6) );

nextORG = nextORG + 1;

S2 = textscan( S0{2}, '%5c%f%f%1c' );

N2 = size( S2{1}, 1 );

STA( nextSTA:nextSTA+N2-1, : ) = S2{1};

ARV( nextSTA:nextSTA+N2-1, 1 ) = S2{2};

PHA( nextSTA:nextSTA+N2-1, 1 ) = S2{4};

EVD( nextSTA:nextSTA+N2-1, : ) = S1{1}(end);

nextSTA = nextSTA + N2;

end

if N >= nextSTA

STA( STA == '-' ) = [];

STA = reshape( STA, [],5 );

ARV( nextSTA : end ) = [];

PHA( nextSTA : end ) = [];

EVD( nextSTA : end ) = [];

end

end

>> filespec = 'h:\m\cssm\community_edit_1M.txt';

>> tic,[ORG0,ARV0,STA0,PHA0,EVD0] = cssm( filespec ); toc

Elapsed time is 22.443859 seconds.

## 14 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406743

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406743

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406744

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406744

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406781

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406781

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406802

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406802

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406811

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406811

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406812

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406812

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406928

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406928

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406938

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_406938

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407064

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407064

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407114

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407114

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407274

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407274

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407292

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407292

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407313

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407313

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407434

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/312599-how-do-i-parse-this-complex-text-file-with-textscan#comment_407434

Sign in to comment.