How to limit a string?

6 ビュー (過去 30 日間)
Ibro Tutic
Ibro Tutic 2015 年 11 月 25 日
コメント済み: Rena Berman 2021 年 5 月 6 日
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
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
Rena Berman
Rena Berman 2021 年 5 月 6 日
(Answers Dev) Restored edit

サインインしてコメントする。

採用された回答

dpb
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.
  1 件のコメント
Ibro Tutic
Ibro Tutic 2015 年 11 月 25 日
I think I have it figured out, thanks.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by