{"id":9276,"date":"2018-07-20T09:00:15","date_gmt":"2018-07-20T13:00:15","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=9276"},"modified":"2018-07-19T14:12:13","modified_gmt":"2018-07-19T18:12:13","slug":"is-net-available","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2018\/07\/20\/is-net-available\/","title":{"rendered":"Is Net Available"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64956\">isnetavl<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3073010\">Rik Wisselink<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>I was recently writing a function that downloads data from a public location online.  I had to call it multiple times to get<br \/>\n      data for different time ranges but wanted to safe guard against there being no internet connection.  If the system failed<br \/>\n      to download data, I wanted to be able to add the internet connectivity information to the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mexception-class.html\">exception<\/a>.\n   <\/p>\n<p>Rik&#8217;s <tt>isnetavl<\/tt> function, which is an enhancement to <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/50498\">another version<\/a> without Unix support, does this.\n   <\/p>\n<p>This is roughly what the code I ended up with looks like.  To run this, my computer is in airplane mode.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    <span style=\"color: #228B22\">% Loop over dates, storing output for each one.<\/span>\r\n    <span style=\"color: #0000FF\">for<\/span> ii = numel(dates):-1:1\r\n        data{ii} = string(webread(url, <span style=\"color: #A020F0\">'date'<\/span>, dates(ii)));\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n<span style=\"color: #0000FF\">catch<\/span> ME\r\n    <span style=\"color: #228B22\">% Add information about specific query.<\/span>\r\n    failedquery = MException(<span style=\"color: #A020F0\">'DataReader:FailedQuery'<\/span>, <span style=\"color: #A020F0\">'url: %s\\ndate: %s'<\/span>, url, char(dates(ii)));\r\n    ME = ME.addCause(failedquery);\r\n\r\n    <span style=\"color: #228B22\">% If the internet is not available, add more information before<\/span>\r\n    <span style=\"color: #228B22\">% rethrowing the exception.<\/span>\r\n    <span style=\"color: #0000FF\">if<\/span> ~isnetavl\r\n        nointernet = MException(<span style=\"color: #A020F0\">'System:NoInternetConnection'<\/span>, <span style=\"color: #A020F0\">'The system is not connected to the internet.'<\/span>);\r\n        ME = ME.addCause(nointernet);\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n\r\n    <span style=\"color: #228B22\">% Rethrow the exception with additional causes<\/span>\r\n    rethrow(ME)\r\n\r\n<span style=\"color: #0000FF\">end<\/span><\/pre>\n<pre style=\"font-style:oblique; color: #FF0000\">Error using webread (line 122)\r\nThe IP address of \"www.mathworks.com\" could not be determined.\r\nError in mainisnetavl (line 28)\r\n        data{ii} = string(webread(url, 'date', dates(ii)));\r\nCaused by:\r\n    url: https:\/\/www.mathworks.com\r\n    date: 04-Jul-2017 13:00:00\r\n    The system is not connected to the internet<\/pre>\n<p><b>Caution:<\/b> Though this function tells you the internet connectivity of your computer, it may not reflect the internet connectivity of<br \/>\n      MATLAB. MATLAB may have its own <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_env\/web-browsers-and-matlab.html#bq37bb3-3\">proxy settings<\/a> that interfere with its access to the internet.\n   <\/p>\n<h3>Comments<a name=\"3\"><\/a><\/h3>\n<p>Is connectivity of MATLAB to the internet something you would like to have built-in, perhaps as part of the <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/call-wsdl-web-services_bu9hx2b-1.html (link no longer works)\">HTTP Interface<\/a>?  If so, please provide your use case <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=9276#respond\">here<\/a>.\n   <\/p>\n<p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=9276#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64956#comments\">comment<\/a> for Rik.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_26417500343a496bb02b6845a9fcc2ca() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='26417500343a496bb02b6845a9fcc2ca ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 26417500343a496bb02b6845a9fcc2ca';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Sean de Wolski';\n        copyright = 'Copyright 2018 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }   \n      \n-->\n<\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_26417500343a496bb02b6845a9fcc2ca()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n            the MATLAB code<br \/>\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2018a<\/p>\n<\/div>\n<p><!--\n26417500343a496bb02b6845a9fcc2ca ##### SOURCE BEGIN #####\n%% Is Net Available\n%\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64956 isnetavl> by\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3073010 Rik Wisselink>.\n% \n\n%% \n%\n% I was recently writing a function that downloads data from a public\n% location online.  I had to call it multiple times to get data for\n% different time ranges but wanted to safe guard against there being no\n% internet connection.  If the system failed to download data, I wanted to\n% be able to add the internet connectivity information to the\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/mexception-class.html\n% exception>.\n%\n% Rik's |isnetavl| function, which is an enhancement to\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/50498 another\n% version> without Unix support, does this.\n%\n% This is roughly what the code I ended up with looks like.  To run this,\n% my computer is in airplane mode.\n\ntry\n    % Loop over dates, storing output for each one.\n    for ii = numel(dates):-1:1\n        data{ii} = string(webread(url, 'date', dates(ii)));\n    end\ncatch ME    \n    % Add information about specific query.\n    failedquery = MException('DataReader:FailedQuery', 'url: %s\\ndate: %s', url, char(dates(ii)));\n    ME = ME.addCause(failedquery);\n    \n    % If the internet is not available, add more information before\n    % rethrowing the exception.\n    if ~isnetavl\n        nointernet = MException('System:NoInternetConnection', 'The system is not connected to the internet.');\n        ME = ME.addCause(nointernet);\n    end\n    \n    % Rethrow the exception with additional causes\n    rethrow(ME)\n\nend\n\n%%\n% *Caution:* Though this function tells you the internet connectivity of\n% your computer, it may not reflect the internet connectivity of MATLAB.\n% MATLAB may have its own\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_env\/web-browsers-and-matlab.html#bq37bb3-3\n% proxy settings> that interfere with its access to the internet.\n%\n\n%% Comments\n% Is connectivity of MATLAB to the internet something you would like to\n% have built-in, perhaps as part of the\n% <https:\/\/www.mathworks.com\/help\/matlab\/http-interface.html HTTP\n% Interface>?  If so, please provide your use case\n% <https:\/\/blogs.mathworks.com\/pick\/?p=9276#respond here>.\n% \n% Give it a try and let us know what you think\n% <https:\/\/blogs.mathworks.com\/pick\/?p=9276#respond here> or leave a\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64956#comments\n% comment> for Rik.\n##### SOURCE END ##### 26417500343a496bb02b6845a9fcc2ca\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sean&#8216;s pick this week is isnetavl by Rik Wisselink.<\/p>\n<p>I was recently writing a function that downloads data from a public location online.  I had to call it multiple times to get<br \/>\n&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2018\/07\/20\/is-net-available\/\">read more >><\/a><\/p>\n","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9276"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=9276"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9276\/revisions"}],"predecessor-version":[{"id":9290,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9276\/revisions\/9290"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=9276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=9276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=9276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}