File Exchange Pick of the Week

Our best user submissions

Is Net Available

Sean‘s pick this week is isnetavl by Rik Wisselink.

I was recently writing a function that downloads data from a public location online. I had to call it multiple times to get
data for different time ranges but wanted to safe guard against there being no internet connection. If the system failed
to download data, I wanted to be able to add the internet connectivity information to the exception.

Rik’s isnetavl function, which is an enhancement to another version without Unix support, does this.

This is roughly what the code I ended up with looks like. To run this, my computer is in airplane mode.

    % Loop over dates, storing output for each one.
    for ii = numel(dates):-1:1
        data{ii} = string(webread(url, 'date', dates(ii)));
catch ME
    % Add information about specific query.
    failedquery = MException('DataReader:FailedQuery', 'url: %s\ndate: %s', url, char(dates(ii)));
    ME = ME.addCause(failedquery);

    % If the internet is not available, add more information before
    % rethrowing the exception.
    if ~isnetavl
        nointernet = MException('System:NoInternetConnection', 'The system is not connected to the internet.');
        ME = ME.addCause(nointernet);

    % Rethrow the exception with additional causes

Error using webread (line 122)
The IP address of "" could not be determined.
Error in mainisnetavl (line 28)
        data{ii} = string(webread(url, 'date', dates(ii)));
Caused by:
    date: 04-Jul-2017 13:00:00
    The system is not connected to the internet

Caution: Though this function tells you the internet connectivity of your computer, it may not reflect the internet connectivity of
MATLAB. MATLAB may have its own proxy settings that interfere with its access to the internet.


Is connectivity of MATLAB to the internet something you would like to have built-in, perhaps as part of the HTTP Interface? If so, please provide your use case here.

Give it a try and let us know what you think here or leave a comment for Rik.

Published with MATLAB® R2018a

  • print


コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。