How to get specific data with an input, without using eval command
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2012-7-6
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.
采纳的回答
Matt Kindig
2012-7-6
编辑:Matt Kindig
2012-7-6
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!