How to get specific data with an input, without using eval command

3 visualizaciones (últimos 30 días)
Greetings,
I am developing a program on MATLAB that takes a forecast data from a web page, ignores de html tags and imprints it on MATLAB memory as a cell array.
The program goes like this:
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);
for ii = 1:1:n
if isempty ( out{ii} ); % ignores the empty lines
out{ii} = [];
else
end
end
If you run the program you will see the cell array.
I am trying to tell MATLAB to give the user specific lines teling the prediction of a certaing time of day.
For instance, if I write 'U' in the Command Window, refering to Tuesday, MATLAB should tell the user:
TUESDAY
EAST WINDS 13 TO 17 KNOTS. SEAS 3 TO 5 FEET. ISOLATED
THUNDERSTORMS.
So far I have develop this code for input output commands,
time=input('Input your forecast interest( (R)REST OF TODAY / (T)TONIGHT / (U)TUESDAY / (UN)TUESDAY NIGHT / (W)WEDNESDAY / (TH)THURSDAY / (F)FRIDAY )\n: ','s');
for ii=1:1:n
if isappdata ( 'R', ); % isappdata (h,name); I dont know what to put on %the name part of isappdata.
disp(out{12}); % html lines
disp(out{13});
disp(out{14});
end
end
But since its a forecast page the html lines change daily.
I need a command to tell MATLAB to extract the desired forecast from the URL and display it to the user, keeping in mind that the page upgrades daily. My boss keeps suggesting me to use the eval command, but I am trying my hard to not use it.
What do you suggest me to do?
Thank you for your time,
JJR
  1 comentario
Walter Roberson
Walter Roberson el 6 de Jul. de 2012
What is your intention in using isappdata() ? You have not created any appdata using setappdata().
You have also not done any parsing of your input file to figure out which lines of the file refers to which time period.

Iniciar sesión para comentar.

Respuesta aceptada

Matt Kindig
Matt Kindig el 6 de Jul. de 2012
Editada: Matt Kindig el 6 de Jul. de 2012
Hi Juan,
I agree that you should be able to do it without eval. However, I am still a little unclear how you want your code to behave. Can you clarify a few things:
1. I ran your code, and based on the data, there is no "THURSDAY" or "FRIDAY" field. How you want your application to respond in that case?
2. Today, for example, is Friday. Thus, do you want a response of 'R' (rest of today) and 'F' (Friday) to give the same response (i.e. 'R' and 'F' for today are the same), or do you want 'F' to work only if 'FRIDAY' explicitly is present in the webpage?
The functions isappdata, setappdata and getappdata are used, for the most part, to assign data to graphics handles. It is not appropriate to use these functions for your purpose.
Also, unless you need the contents of the webpage to be written to file, you can simply use urlread() to directly read the contents into a variable, and then use regexp to split the text line by line).
Regards, Matt
  2 comentarios
Juan Rosado
Juan Rosado el 6 de Jul. de 2012
My mistake Matt,
Since the page upgrades daily, the headings for Tonight, Friday, etc changes as well. The day I developed the code was Monday and the Forecast for that day had headings of: Tonight, Rest of Day, Tuesday, Wednesday, Thursday and Friday. I forgot to correct that on the input variable.
Also, could you give me an example how can I use regexp to split the text line by line like you are suggesting.
Once againg thank you for your time.
Walter Roberson
Walter Roberson el 6 de Jul. de 2012
lines = regexp(S, '\n', 'split');

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Call Web Services from MATLAB Using HTTP en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by