Clear Filters
Clear Filters

retrieving solar irradiance data through webread function

1 view (last 30 days)
how can i retrieve solar irradiance data of 24 hours from https://www.newquayweather.com/pwsWD/index.php in MATLAB through webread function?

Accepted Answer

Cameron
Cameron on 9 Mar 2023
Edited: Cameron on 9 Mar 2023
This is a way to get it instantaneously.
url = 'https://www.newquayweather.com/pwsWD/index.php';
w = webread(url);
loc1 = strfind(w,'W/m²');
B = str2num(string(regexp(w(loc1-10:loc1),'\d*','Match')));
fprintf('Solar irradiance at %s is %d W/m²',datetime('now'),B)
Solar irradiance at 09-Mar-2023 15:18:03 is 174 W/m²
  4 Comments
Cameron
Cameron on 10 Mar 2023
There are a couple of things you can do. The reason I didn't plot the tsi vs date is because if you were to close the graph, the program would stop. You can either run the script until 1 whole day has passed or you can hit Control+C to manually stop the program. The variables for dt and tsi should be in your workspace up until you stopped the program. The code I will paste below will save your data to a .txt file you specify prior to reading data from the website. So you can open this file to see all the data that's been collected over a period of time. I did notice while I was running this script that sometimes the url said the solar irradiance was 0 W/m2 which wasn't true, but that's what was on the url so that's what was read by the program.
clear
url = 'https://www.newquayweather.com/pwsWD/index.php'; %your url
[file,path,indx] = uiputfile('.txt'); %save a file as a .txt. you can change this to your preferred extension
if indx == 0; msg = 'No path selected.'; error(msg); end %user hits Cancel or closes uiputfile prompt
cd(path) %change path to the one designated in uiputfile
fileID = fopen(file,'a'); %open file and append any data
SecBetweenReading = 10; %seconds between each reading
opt = weboptions('Timeout',60); %set the web timeout to 60 seconds
fig = figure; %create figure
ax = uiaxes(fig); %create axes
for x = 1:3600*24/SecBetweenReading %3600 seconds in an hour, 24 hours divided by seconds between reading
w = webread(url,opt); %read the web page
loc1 = strfind(w,'W/m²'); %find where W/m2 is
B = str2num(string(regexp(w(loc1-10:loc1),'\d*','Match'))); %get only the number
dt(x,1) = datetime('now'); %save datetime
tsi(x,1) = B; %save your solar irradiance
fprintf(fileID,'%s,%d\n',dt(x,1),tsi(x,1));
disp(dt(end,1)) %display the most recent time the data was read
disp(tsi(end,1)) %display the most recent tsi the data was read
if isgraphics(ax) %if you have not closed the plot
plot(ax,dt,tsi,'-o') %plot all data
end
pause(SecBetweenReading) %pause for a given amount of time
end
fclose(fileID);
If you're having difficulties deleting files, you may have not used the fclose() function on one of your .txt files. I would use
fclose('all')
if you're getting an error from your computer saying MATLAB has a certain file open.

Sign in to comment.

More Answers (0)

Categories

Find more on Live Scripts and Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!