if Statement for text files

1 view (last 30 days)
Juan Rosado
Juan Rosado on 10 Jul 2012
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 Comments
Juan Rosado
Juan Rosado on 10 Jul 2012
Jan, my humble apologies. Thank you for the pointer, it won't happen again.
Jan
Jan on 15 Jul 2012
Edited: Jan on 15 Jul 2012
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!

Sign in to comment.

Accepted Answer

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

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 10 Jul 2012
Edited: Andrei Bobrov on 10 Jul 2012
[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);

Community Treasure Hunt

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

Start Hunting!