Download Web Page and Files

MATLAB® provides two functions for reading content from RESTful Web services: webread and websave. With the webread function, you can read the contents of a Web page to a string variable in the MATLAB workspace. With the websave function, you can save a Web page's content to a file.

Because it can create a string variable in the workspace, the webread function is useful for working with the contents of Web pages in MATLAB. The websave function is useful for saving Web pages to a local folder.

    Note   When webread returns HTML as a string, remember that only the HTML in that specific Web page is retrieved. The hyperlink targets, images, and so on are not retrieved.

If you need to pass parameters to a Web page, the webread and websave functions let you define the parameters as Name, Value pair arguments. For more information, see the webread and websave reference pages.

Example — Use the webread Function

The following procedure demonstrates how to retrieve the contents of the Web page listing the files submitted to the MATLAB Central™ File Exchange, http://www.mathworks.com/matlabcentral/fileexchange/. It assigns the results to a string variable, fullList:

filex = 'http://www.mathworks.com/matlabcentral/fileexchange/';
fullList = webread(filex);

Retrieve a list of only those files uploaded to the File Exchange within the past 7 days that contain the word Simulink®. Set duration and term as parameters that webread passes to the Web page.

filex = 'http://www.mathworks.com/matlabcentral/fileexchange/';
recent = webread(filex,'duration',7,'term','simulink');

Example — Use the websave Function

The following example builds on the procedure in the previous section, but saves the content to a file:

% Locate the list of files at the MATLAB Central File Exchange
% uploaded within the past 7 days, that contain "Simulink."
filex = 'http://www.mathworks.com/matlabcentral/fileexchange/';

% Save the Web content to a file.
recent = websave('contains_simulink.html',filex, ...
    'duration',7,'term','simulink');

MATLAB saves the Web page as contains_simulink.html. The output argument recent contains the full path to contains_simulink.html. Call the web function to display contains_simulink.html in a browser.

web(recent)

This page has links to files uploaded to the MATLAB Central File Exchange.

Was this topic helpful?