Documentation Center

  • Trial Software
  • Product Updates

urlread

Download URL content to MATLAB string

Syntax

  • str = urlread(URL) example
  • str = urlread(URL,Name,Value) example
  • [str,status] = urlread(___)

Description

example

str = urlread(URL) downloads the HTML Web content from the specified URL into the string str. urlread does not retrieve hyperlink targets and images.

example

str = urlread(URL,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

[str,status] = urlread(___) suppresses the display of error messages, using any of the input arguments in the previous syntaxes. When the operation is successful, status is 1. Otherwise, status is 0

Examples

expand all

Download Web Content by Specifying Complete URL

Download the HTML for the page on the MATLAB® Central File Exchange that lists submissions related to urlread.

fullURL = ['http://www.mathworks.com/matlabcentral/fileexchange' ...
	   '?term=urlread'];
str = urlread(fullURL);

urlread reads from the specified URL and downloads the HTML content to the string str.

Download Web Content Related to a Term

Download the HTML for the page on the MATLAB Central File Exchange that lists submissions related to urlread.

URL = 'http://www.mathworks.com/matlabcentral/fileexchange';
str = urlread(URL,'Get',{'term','urlread'});

urlread reads from http://www.mathworks.com/matlabcentral/fileexchange/?term=urlread and downloads the HTML content to the string str.

Specify Timeout Duration

Download content from a page on the MATLAB Central File Exchange as in the first example, and specify a timeout duration of 5 seconds.

fullURL = ['http://www.mathworks.com/matlabcentral/fileexchange' ...
	   '?term=urlread'];
str = urlread(fullURL,'Timeout',5);

Input Arguments

expand all

URL — Content locationstring

Content location, specified as a string. Include the transfer protocol, such as http, ftp, or file.

Example: 'http://www.mathworks.com/matlabcentral'

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Timeout',10,'Charset','UTF-8' specifies that urlread should time out after 10 seconds, and the character encoding of the file is UTF-8.

'Get' — Data to send to the Web form using the GET methodcell array

Parameters of the data to send to the Web form using the GET method, specified as the comma-separated pair consisting of 'get' and a cell array of paired parameter names and values. The supported parameters depend upon the URL.

'Get' includes the data in the URL, separated by ? and & characters.

Example: 'Get',{'term','urlread'}

'Post' — Data to send to the Web form using the POST methodcell array

Parameters of the data to send to the Web form using the POST method, specified as the comma-separated pair consisting of 'post' and a cell array of paired parameter names and values. The supported parameters depend upon the URL.

'Post' submits the data as part of the request headers, not explicitly in the URL.

'Charset' — Character encodingstring

Character encoding, specified as the comma-separated pair consisting of 'Charset' and a string. If you do not specify Charset, the function attempts to determine the character encoding from the headers of the file. If the character encoding cannot be determined, Charset defaults to the native encoding for the file protocol, and UTF-8 for all other protocols.

Example: 'Charset','ISO-8859-1'

'Timeout' — Timeout durationscalar

Timeout duration in seconds, specified as the comma-separated pair consisting of 'Timeout' and a scalar. The timeout duration determines when the function errors rather than continues to wait for the server to respond or send data.

Example: 'Timeout',10

'UserAgent' — Client user agent identificationstring

Client user agent identification, specified as the comma-separated pair consisting of 'UserAgent' and a string.

Example: 'UserAgent','MATLAB R2012b'

'Authentication' — HTTP authentication mechanism'Basic'

HTTP authentication mechanism, specified as the comma-separated pair consisting of 'Authentication' and a string. Currently, only the value 'Basic' is supported. 'Authentication','Basic' specifies basic authentication.

If you include the Authentication argument, you must also include the Username and Password arguments.

'Username' — User identifierstring

User identifier, specified as the comma-separated pair consisting of 'Username' and a string. If you include the Username argument, you must also include the Password and Authentication arguments.

Example: 'Username','myName'

'Password' — User authentication passwordstring

User authentication password, specified as the comma-separated pair consisting of 'Password' and a string. If you include the Password argument, you must also include the Username and Authentication arguments.

Example: 'Password','myPassword123'

Output Arguments

expand all

str — Contents of the file at the specified URLstring

Contents of the file at the specified URL, returned as a string. For example, if the URL corresponds to an HTML page, str contains the text and markup in the HTML file. If the URL corresponds to a binary file, str is not readable.

status — Download status1 | 0

Download status, returned as either 1 or 0. When the download is successful, status is 1. Otherwise, status is 0.

More About

expand all

Tips

  • urlread saves Web content to a string. To save content to a file, use urlwrite.

  • urlread and urlwrite can download content from FTP sites. Alternatively, use the ftp function to connect to an FTP server and the mget function to download a file.

See Also

| | |

Was this topic helpful?