File Exchange Pick of the Week

Our best user submissions

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Is Net Available

Posted by Sean de Wolski,

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.

try
    % Loop over dates, storing output for each one.
    for ii = numel(dates):-1:1
        data{ii} = string(webread(url, 'date', dates(ii)));
    end
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);
    end

    % Rethrow the exception with additional causes
    rethrow(ME)

end
Error using webread (line 122)
The IP address of "www.mathworks.com" could not be determined.
Error in mainisnetavl (line 28)
        data{ii} = string(webread(url, 'date', dates(ii)));
Caused by:
    url: https://www.mathworks.com
    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.

Comments

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.

Get the MATLAB code

Published with MATLAB® R2018a

Add A Comment

Your email address will not be published. Required fields are marked *

Preview: hide