I am trying to write a simple code which allows me to access a specific webpage at a very exact time (precise upon a few milliseconds). In the code below I use the variable 'timerbrow' as the time (in seconds) for the code and the browser to open the webpage. At the moment I have to guess this time manually. I was wondering if this time can be found automatically? For example by pinging the website through MatLab?
Thanks in advance
close all, clear all;
%%%give in time it takes for browser to load.
timerbrow=1;
runsec=60-timerbrow;
Dsec=1; Dminute=1;
while Dsec>0.001 && Dminute>0.001
c=clock;
Dminute=59-c(5);
Dsec=runsec-c(6);
end
%%%give in the url
url='https://www.google.com';
[stat,h]=web(url,'-browser');

4 Comments

Why use the while loop, and not use a plain pause(runsec)?
Also, I don't understand what you want with respect to your timing.
The ping can be easily implemented with a call to system:
function [connected,timing]=get_response_time(url_or_ip)
if ispc
[~,b]=system(['ping -n 1 ' url_or_ip]);
n=strfind(b,'Lost');
n1=b(n+7);
if(n1=='0')
connected=1;
if nargout==2
n=strfind(b,'time=');m=strfind(b,'ms');m=m(m>n);m=m(1)-1;
timing=str2double(b((n+5):m));
end
else
connected=0;
timing=0;
end
elseif isunix
[~,b]=system(['ping -c 1 ' url_or_ip]);
n=strfind(b,'received');
n1=b(n-2);
if(n1=='1')
connected=1;
if nargout==2
n=strfind(b,'/');n=n([end-1 end]);
timing=str2double(b(n(1):n(2)));
end
else
connected=0;
timing=0;
end
else
error('How did you even get Matlab to work?')
end
(I use this to ping to 8.8.8.8 to see if there is an internet connection)
Thank you for the quick answer, Rik. I might have expressed myself in a wrong way. The idea of the script is that I can run it at a certain time (let's say 12:35) and that it would execute at a precise time (in this case, as written in the code I gave, it would be at 12:59). This is why the while loop is implemented. The script keeps running and checking the time until it is the given time (12:59) to leave the while loop and open the webpage.
However, I want the webpage to open at precisely the given time (12:59) instead of the code to start opening the webpage at given time (12:59). This is why I put in the variable "timerbrow" for the code to start opening the webpage at the 'given time' minus 'timerbrow' (12:59 - timerbrow [seconds]) in order for the webpage to precisely open at the given time (12:59) (this is for sure a matter of a couple of milliseconds).
I was wondering if there is a way to write a code that would allow me to find the exact value of "timerbrow". Hence, a code that would give me the exact time needed to open a webpage.
Rik
Rik on 28 Jan 2018
A pause would do the same thing as your loop, but not use as much computational resources.
The time to load a page can be substantially more than just the ping. Just testing it might be the only solution.
I see. Ok, thank you!

Sign in to comment.

 Accepted Answer

Jan
Jan on 28 Jan 2018

2 votes

"A few milliseconds" is a very low limit. It will be hard to get this using plain Matlab, neither by clock nor by pause. See e.g. https://undocumentedmatlab.com/blog/pause-for-the-better for a more precise delay by java code. The FEX: High Accuracy Timer might be useful also.

1 Comment

Thank you. I might need tot take a look at another programming language. I understand a few milliseconds is practically impossible but I'll settle with the most precise option.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 28 Jan 2018

2 votes

The time to serve a web page is quite variable, and depends upon the amount of internet traffic between you and the host (for them to get the request) and upon the load they have right then (for them to process the request) and upon the amount of internet traffic back from them to you. Multiple hosts in very different locations can be involved depending on the exact request -- for example Mathworks uses one of the cloud hosting companies to provide faster response for some kinds of queries.
Your target probably cannot be reasonably met.

1 Comment

Yes, I realize it's a hard goal to achieve. I'll try another programming language. It'll make my project a bit more complex, but I'll manage! I'm loving the related situation about usage peaks though :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!