필터 지우기
필터 지우기

if Statement for text files

조회 수: 1 (최근 30일)
Juan Rosado
Juan Rosado 2012년 7월 10일
Greetings,
I have this cell array that displays this text:
urlwrite('http://weather.noaa.gov/cgi-bin/fmtbltn.pl?file=forecasts/marine/coastal/am/amz725.txt','Water_Coastal_Southern_Puerto_Rico_Out.txt'); % URL from forecast web page
fid=fopen('Water_Coastal_Southern_Puerto_Rico_Out.txt');
y=fgetl(fid);
data = textscan( fid, '%s', 'Delimiter', ''); %read the entire file as strings, one per line.
fclose(fid);
out = regexprep( data{1}, '<[^>]+>', '' ) %remove the HTML
n=length(out);
out =
'CWFSJU'
'COASTAL WATERS FORECAST'
'NATIONAL WEATHER SERVICE SAN JUAN PR'
'1024 PM AST MON JUL 9 2012'
'PUERTO RICO AND U.S. VIRGIN ISLANDS WATERS'
''
'AMZ725-101500-'
'COASTAL WATERS OF SOUTHERN USVI VIEQUES AND EASTERN PUERTO RICO OUT'
'10 NM-'
'1024 PM AST MON JUL 9 2012'
'REST OF TONIGHT'
'EAST WINDS 12 TO 16 KNOTS. SEAS 3 TO 4 FEET.'
'ISOLATED SHOWERS. '
'TUESDAY'
'EAST WINDS 12 TO 16 KNOTS. SEAS 3 TO 4 FEET. HAZE.'
'ISOLATED SHOWERS. '
'TUESDAY NIGHT'
'EAST NORTHEAST WINDS 13 TO 16 KNOTS. SEAS 3 TO'
'4 FEET. HAZE. ISOLATED SHOWERS. '
'WEDNESDAY'
'EAST NORTHEAST WINDS 12 TO 15 KNOTS. SEAS 3 TO 4 FEET.'
'HAZE. ISOLATED SHOWERS. '
'WEDNESDAY NIGHT'
'EAST NORTHEAST WINDS 12 TO 15 KNOTS. SEAS 3 TO'
'4 FEET. HAZE IN THE EVENING. SCATTERED SHOWERS THROUGH THE NIGHT. '
'THURSDAY'
'EAST WINDS 8 TO 13 KNOTS. SEAS 2 TO 4 FEET. ISOLATED'
'SHOWERS. '
'FRIDAY'
'EAST SOUTHEAST WINDS 11 TO 16 KNOTS. SEAS 3 TO 5 FEET.'
'ISOLATED SHOWERS. '
'SATURDAY'
'EAST WINDS 13 TO 18 KNOTS. SEAS 4 TO 6 FEET. ISOLATED'
'SHOWERS. '
[1x165 char]
I want to generate an "if" statement that takes certain lines to display it on the Command Window. For example,
if (the text line starts with 'REST... display the lines:
'REST OF TONIGHT'
'EAST WINDS 12 TO 16 KNOTS. SEAS 3 TO 4 FEET.'
'ISOLATED SHOWERS.'
Thank you for your time.
JJR
  댓글 수: 3
Juan Rosado
Juan Rosado 2012년 7월 10일
Jan, my humble apologies. Thank you for the pointer, it won't happen again.
Jan
Jan 2012년 7월 15일
편집: Jan 2012년 7월 15일
Dear Juan, there is no reason for apologies. Simply edit the question and format the code. This does not help me, but it improves your chances to get a useful answer.
Or you could wait until Walter formats your code. Thanks, Walter!

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2012년 7월 10일
index = find(strncmpi(out, 'Rest', 4));
fprintf('%s\n', out{index + 1});

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2012년 7월 10일
편집: Andrei Bobrov 2012년 7월 10일
[S,S] = weekday(now + (0:6),'long')
i1 = strncmp(out,'REST',4)
idx = i1 | ismember(out,cellstr(upper(S)));
I = cumsum(idx);
out2 = out(I(i1) == I);
or
[S,S] = weekday(now + (0:6),'long')
i1 = find(strncmp(out,'REST',4));
i2 = find(ismember(out,cellstr(upper(S))));
out3 = out(i1:i2(find(i1 < i2,1,'first'))-1);

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by