How to limit a string?

6 visualizaciones (últimos 30 días)
Ibro Tutic
Ibro Tutic el 25 de Nov. de 2015
Comentada: Rena Berman el 6 de Mayo de 2021
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 comentarios
Stephen23
Stephen23 el 26 de Dic. de 2020
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 el 6 de Mayo de 2021
(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 25 de Nov. de 2015
Test for the existence of the complete string and exclude...set those lines in the initial array =[] before processing the rest.
  1 comentario
Ibro Tutic
Ibro Tutic el 25 de Nov. de 2015
I think I have it figured out, thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by