if Statement for text files

1 次查看(过去 30 天)
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 中查找有关 Coastal Engineering 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by