Trying to figure out how to limit a string in terms of characters?
MATLAB: How to limit a string
datalimiting stringsstringstext;textscan
Related Solutions
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'; % avoid magic number
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}; % one based
endmean_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}; % one based endrange_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}; % one based 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'; % avoid magic numberlen = 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 ); % one based
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); % one based
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); % one based
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 = { ... format keyword '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 endendfunction 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 ); % one based matrix( ix_linear ) = num(:,3); matrix = reshape( matrix, sz );endfunction 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); % one based endfunction 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{:}; endfunction ism = is_member( keyword, rows ) % the keyword is followed by either ":" or "["
cac = regexp( rows, ['^',keyword,'(?=(:|\[))'], 'once' ); ism = not( cellfun( @isempty, cac ) );end
Hello,
Here is an solution that you can refer,
clearFileName = 'HP1_ordered.txt';NewFile = 'HP1_ordered_skip10.txt';fp = fopen(FileName);Str = textscan(fp,'%s','delimiter',sprintf('\f'));fclose(fp);cStr = char(Str{1}(1:10:end)); % you can change number of row
% that you want to skip at this line.
fid = fopen(NewFile, 'w');if fid == -1 error('Cannot create new file: %s', NewFile);endfor i = 1:size(cStr,1)fprintf(fid, '%s\n', cStr(i,:));endfclose(fid);
In this code, it is supposed that you have "FileName" and want to write its containts into "NewFile". Also, you want to skip 10 rows of the "FileName".
Hope it could help.
Best Answer