How to limit a string?
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I have some data that I am reading from a text file, and some of the data is PowerCylinderTemperatureHistogram and some is PowerCylinderTemperatureHistogramBreakpoints. Now when my code is filtering through the text, it associates the PowerCylinderTemperatureHistogramBreakpoints with PowerCylinderTemperatureHistogram, how can I limit that text that is chooses to JUST PowerCylinderTemperatureHistogram and not the Breakpoints?
clear all;
close all
clc
projectdir = 'C:\Users\it58528\Documents\Power Cylinder Temp and Rainflow Cycle Counter - After 16500 Cycles - 2015-10-12.prm';
fid = fopen(projectdir);
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 );
%
str = 'PowerCylinderTemperatureHistogram';
len = length (str);
is_temp = strncmp ( str, rows, len );
temp_rows = rows ( is_temp);
%
str = 'PowerCylinderTemperatureHistogramBreakpoints';
len = length (str);
is_break = strncmp ( str, rows, len );
break_rows = rows ( is_break);
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
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}; % one based
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}; % one based
end
temp_matrix = nan ( 1, 12 );
for jj = 1 : length ( temp_rows )
%
cac = textscan( temp_rows{jj}, '%*s%d%f' ...
, 'Delimiter' , ' []:' ...
, 'MultipleDelimsAsOne', true );
%
temp_matrix( 1, cac{1}+1 ) = cac{2}; %one based
end
temp_vector = nan ( 1, 11 );
for jj = 1 : length ( break_rows )
%
cac = textscan( break_rows{jj}, '%*s%d%f' ...
, 'Delimiter' , ' []:' ...
, 'MultipleDelimsAsOne', true );
%
temp_vector( 1, cac{1}+1 ) = cac{2};
end
2 个评论
Stephen23
2020-12-26
Original question by Ibro Tutic on 25th November 2015 retrieved from Google Cache:
How to limit a string?
Hi,
I have some data that I am reading from a text file, and some of the data is PowerCylinderTemperatureHistogram and some is PowerCylinderTemperatureHistogramBreakpoints. Now when my code is filtering through the text, it associates the PowerCylinderTemperatureHistogramBreakpoints with PowerCylinderTemperatureHistogram, how can I limit that text that is chooses to JUST PowerCylinderTemperatureHistogram and not the Breakpoints?
clear all;
close all
clc
projectdir = 'C:\Users\it58528\Documents\Power Cylinder Temp and Rainflow Cycle Counter - After 16500 Cycles - 2015-10-12.prm';
fid = fopen(projectdir);
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 );
%
str = 'PowerCylinderTemperatureHistogram';
len = length (str);
is_temp = strncmp ( str, rows, len );
temp_rows = rows ( is_temp);
%
str = 'PowerCylinderTemperatureHistogramBreakpoints';
len = length (str);
is_break = strncmp ( str, rows, len );
break_rows = rows ( is_break);
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
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}; % one based
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}; % one based
end
temp_matrix = nan ( 1, 12 );
for jj = 1 : length ( temp_rows )
%
cac = textscan( temp_rows{jj}, '%*s%d%f' ...
, 'Delimiter' , ' []:' ...
, 'MultipleDelimsAsOne', true );
%
temp_matrix( 1, cac{1}+1 ) = cac{2}; %one based
end
temp_vector = nan ( 1, 11 );
for jj = 1 : length ( break_rows )
%
cac = textscan( break_rows{jj}, '%*s%d%f' ...
, 'Delimiter' , ' []:' ...
, 'MultipleDelimsAsOne', true );
%
temp_vector( 1, cac{1}+1 ) = cac{2};
end
采纳的回答
dpb
2015-11-25
Test for the existence of the complete string and exclude...set those lines in the initial array =[] before processing the rest.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!