I have assumed that the size of the resulting arrays are known

fid = fopen( 'c:\m\cssm\test4.txt' );

rows = textscan( fid, '%s', 'Delimiter', '\n' );

fclose( fid );

rows = rows{:};

str = 'RainflowCycleCounterHistogram';

len = length( str );

is_counter = strncmp( str, rows, len );

counter_rows = rows( is_counter );

str = 'RainflowCycleMeanBreakpoints';

len = length( str );

is_mean = strncmp( str, rows, len );

mean_rows = rows( is_mean );

str = 'RainflowCycleRangeBreakpoints';

len = length( str );

is_range = strncmp( str, rows, len );

range_rows = rows( is_range );

counter_matrix = nan( 10, 10 );

for jj = 1 : length( counter_rows )

cac = textscan( counter_rows{jj}, '%*s%d%d%f' ...

, 'Delimiter' , ' []:' ...

, 'MultipleDelimsAsOne', true );

counter_matrix( cac{1}+1, cac{2}+1 ) = cac{3};

end

mean_vector = nan( 1, 10 );

for jj = 1 : length( mean_rows )

cac = textscan( mean_rows{jj}, '%*s%d%f' ...

, 'Delimiter' , ' []:' ...

, 'MultipleDelimsAsOne', true );

mean_vector( 1, cac{1}+1 ) = cac{2};

end

range_vector = nan( 1, 10 );

for jj = 1 : length( range_rows )

cac = textscan( range_rows{jj}, '%*s%d%f' ...

, 'Delimiter' , ' []:' ...

, 'MultipleDelimsAsOne', true );

range_vector( 1, cac{1}+1 ) = cac{2};

end

 

or maybe better - no assumptions regarding sizes

fid = fopen( 'c:\m\cssm\test4.txt' );

rows = textscan( fid, '%s', 'Delimiter', '\n' );

fclose( fid );

rows = rows{:};

str = 'RainflowCycleCounterHistogram';

len = length( str );

is_counter = strncmp( str, rows, len );

counter_rows = rows( is_counter );

str = 'RainflowCycleMeanBreakpoints';

len = length( str );

is_mean = strncmp( str, rows, len );

mean_rows = rows( is_mean );

str = 'RainflowCycleRangeBreakpoints';

len = length( str );

is_range = strncmp( str, rows, len );

range_rows = rows( is_range );

CRS = permute( char( counter_rows ), [2,1] );

cac = textscan( CRS, '%*s%f%f%f' ...

, 'Delimiter' , '[]: '...

, 'MultipleDelimsAsOne' , true ...

, 'CollectOutput' , true );

num = cac{1};

sz1 = min( num(:,1:2), [], 1 );

sz2 = max( num(:,1:2), [], 1 );

sz = sz2-sz1+[1,1];

ix_linear = sub2ind( sz, num(:,1)+1, num(:,2)+1 );

counter_matrix( ix_linear ) = num(:,3);

counter_matrix = reshape( counter_matrix, sz );

MRS = permute( char( mean_rows ), [2,1] );

cac = textscan( MRS, '%*s%f%f' ...

, 'Delimiter' , '[]: '...

, 'MultipleDelimsAsOne' , true ...

, 'CollectOutput' , true );

num = cac{1};

mean_vector( num(:,1)+1 ) = num(:,2);

RRS = permute( char( range_rows ), [2,1] );

cac = textscan( RRS, '%*s%f%f' ...

, 'Delimiter' , ' []:'...

, 'MultipleDelimsAsOne' , true ...

, 'CollectOutput' , true );

range_vector( num(:,1)+1 ) = num(:,2);

hope they return identical results :-)

 

and another iteration

Comments:

- A function is superior to a script. It doesn't mess with the base workspace. It's easier to debug and it's easier to call from a script or function.
- This function is readable. It's fairly straightforward to add new keywords and row formats.
- The switch case can be replaced by a feval construct. But why do that?
- The subfunctions, f1, f2 and f3, have large parts of their code in common. That asks for further refactoring.
- Allocating a separate sub-function to each type of row makes testing easier.
- If speed becomes a problem analyze the code with the profiler.

>> S = cssm( 'c:\m\cssm\text4.txt' )

S =

RainflowCycleCounterHistogram: [10x10 double]

RainflowCycleMeanBreakpoints: [-111 100 300 330 360 380 390 400 410 420]

RainflowCycleRangeBreakpoints: [0 35 70 100 135 170 200 230 260 300]

RainflowCycleReversalTolerance: 20

PowerCylinderTemperature: 0

PowerCylinderTemperatureHistogram: [1x12 double]

PowerCylinderTemperatureHistogramBreakpoints: [0 150 175 200 220 250 300 320 350 370 400]

>>

where

function S = cssm( filespec )

fid = fopen( filespec );

rows = textscan( fid, '%s', 'Delimiter', '\n' );

fclose( fid );

rows = strtrim( rows{:} );

type_list = {

...

'f1', 'RainflowCycleCounterHistogram'

'f2', 'RainflowCycleMeanBreakpoints'

'f2', 'RainflowCycleRangeBreakpoints'

'f3', 'RainflowCycleReversalTolerance'

'f3', 'PowerCylinderTemperature'

'f2', 'PowerCylinderTemperatureHistogram'

'f2', 'PowerCylinderTemperatureHistogramBreakpoints'

};

for jj = 1 : size( type_list, 1 )

switch type_list{jj,1}

case 'f1'

S.(type_list{jj,2}) = f1( type_list{jj,2}, rows );

case 'f2'

S.(type_list{jj,2}) = f2( type_list{jj,2}, rows );

case 'f3'

S.(type_list{jj,2}) = f3( type_list{jj,2}, rows );

otherwise

error( 'The format, "%s", is not yet implemented', type_list{jj,1} )

end

end

end

function matrix = f1( keyword, rows )

ism = is_member( keyword, rows );

cur_rows = rows( ism );

str = permute( char( cur_rows ), [2,1] );

cac = textscan( str, '%*s%f%f%f' ...

, 'Delimiter' , '[]: '...

, 'MultipleDelimsAsOne' , true ...

, 'CollectOutput' , true );

num = cac{1};

sz1 = min( num(:,1:2), [], 1 );

sz2 = max( num(:,1:2), [], 1 );

sz = sz2-sz1+[1,1];

ix_linear = sub2ind( sz, num(:,1)+1, num(:,2)+1 );

matrix( ix_linear ) = num(:,3);

matrix = reshape( matrix, sz );

end

function matrix = f2( keyword, rows )

ism = is_member( keyword, rows );

cur_rows = rows( ism );

str = permute( char( cur_rows ), [2,1] );

cac = textscan( str, '%*s%f%f' ...

, 'Delimiter' , '[]: '...

, 'MultipleDelimsAsOne' , true ...

, 'CollectOutput' , true );

num = cac{1};

matrix( num(:,1)+1 ) = num(:,2);

end

function matrix = f3( keyword, rows )

ism = is_member( keyword, rows );

cur_rows = rows( ism );

str = permute( char( cur_rows ), [2,1] );

cac = textscan( str, '%*s%f', 'Delimiter',':' );

matrix = cac{:};

end

function ism = is_member( keyword, rows )

cac = regexp( rows, ['^',keyword,'(?=(:|\[))'], 'once' );

ism = not( cellfun( @isempty, cac ) );

end

## 6 Comments

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325472

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325472

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325541

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325541

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325590

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325590

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325595

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325595

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325630

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_325630

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_1228197

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/257283-converting-unformatted-text-to-formatted-text#comment_1228197

Sign in to comment.