How to limit a string?
조회 수: 2 (최근 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!