| MATLAB® | ![]() |
| On this page… |
|---|
Downloading Web Content and Files |
MATLAB software provides functions for exchanging files over the Internet. You can exchange files using common protocols, such as File Transfer Protocol (FTP), Simple Mail Transport Protocol (SMTP), and HyperText Transfer Protocol (HTTP). In addition, you can create zip archives to minimize the transmitted file size, and also save and work with Web pages.
MATLAB provides two functions for downloading Web pages and files using HTTP: urlread and urlwrite. With the urlread function, you can read and save the contents of a Web page to a string variable in the MATLAB workspace. With the urlwrite function, you can save a Web page's content to a file.
Because it creates a string variable in the workspace, the urlread function is useful for working with the contents of Web pages in MATLAB. The urlwrite function is useful for saving Web pages to a local directory.
Note When using urlread, 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 urlread and urlwrite functions let you use HTTP post and get methods. For more information, see the urlread and urlwrite reference pages.
The following procedure demonstrates how to retrieve the contents of the Web page containing the Recent File list at the MATLAB Central File Exchange, http://www.mathworks.com/matlabcentral/fileexchange/index.jsp. It assigns the results to a string variable, recentFile, and it uses the strfind function to search the retrieved content for a specific word:
Retrieve the Web page content with the urlread function:
recentFile =
urlread('http://www.mathworks.com/matlabcentral/fileexchange/
loadFileList.do?objectType=fileexchange&orderBy=date&srt3=0');After retrieving the content, run the strfind function on the recentFile variable:
hits = strfind(recentFile,'Simulink');
If the file contains the word Simulink, MATLAB stores the matches in the hits variable.
While you can manually pass arguments using the URL, the urlread function also lets you pass parameters to a Web page using standard HTTP methods, including post and form. Using the HTTP get method, which passes parameters in the URL, the following code queries Google for the word Simulink:
s =
urlread('http://www.google.com/search','get',{'q','Simulink'})For more information, see the urlread reference page.
The following example builds on the procedure in the previous section. This example still uses urlread and checks for a specific word, but it also uses urlwrite to save the file if it contains any matches:
% The urlread function loads the contents of the Web page into
the % MATLAB workspace.
recentFile =
urlread('http://www.mathworks.com/matlabcentral/fileexchange/
loadFileList.do?objectType=fileexchange&orderBy=date&srt3=0');
% The strfind function searches for the word "Simulink".
hits = strfind(recentFile,'Simulink');
% The if statement checks for any hits.
if ~isempty(hits)
% If there are hits, the Web page will be saved locally
% using the urlwrite function.
urlwrite('http://www.mathworks.com/matlabcentral/fileexchange/
loadFileList.do?objectType=fileexchange&orderBy=date&srt3=0',
'contains_simulink.html');
end;MATLAB saves the Web page as contains_simulink.html.
Using the zip and unzip functions, you can compress and decompress files and directories. The zip function compresses files or directories into a zip archive. The unzip function decompresses zip archives.
Again building on the example from previous sections, the following code creates a zip archive of the retrieved Web page:
% The urlread function loads the contents of the Web page into
the % MATLAB workspace.
recentFile =
urlread('http://www.mathworks.com/matlabcentral/fileexchange/
loadFileList.do?objectType=fileexchange&orderBy=date&srt3=0');
% The strfind function searches for the word "Simulink".
hits = strfind(recentFile,'Simulink');
% The if statement checks for any hits.
if ~isempty(hits)
% If there are hits, the Web page will be saved locally
% using the urlwrite function.
urlwrite('http://www.mathworks.com/matlabcentral/fileexchange/
loadFileList.do?objectType=fileexchange&orderBy=date&srt3=0',
'contains_simulink.html');
% The zip function creates a zip archive of the retrieved Web
page.
zip('simulink_matches.zip','contains_simulink.html');
end;To send an e-mail from MATLAB, use the sendmail function. You can also attach files to an e-mail, which lets you mail files directly from MATLAB. To use sendmail, you must first set up your e-mail address and your SMTP server information with the setpref function.
The setpref function defines two mail-related preferences:
E-mail address: This preference sets your e-mail address that will appear on the message. Here is an example of the syntax:
setpref('Internet','E_mail','youraddress@yourserver.com');
SMTP server: This preference sets your outgoing SMTP server address, which can be almost any e-mail server that supports the Post Office Protocol (POP) or the Internet Message Access Protocol (IMAP). Here is an example of the syntax:
setpref('Internet', 'SMTP_Server', 'mail.server.network');
You should be able to find your outgoing SMTP server address in your e-mail account settings in your e-mail client application. You can also contact your system administrator for the information.
Note The sendmail function does not support e-mail servers that require authentication. |
Once you have properly configured MATLAB, you can use the sendmail function. The sendmail function requires at least two arguments: the recipient's e-mail address and the e-mail subject:
sendmail('recepient@someserver.com', 'Hello From MATLAB!');
You can supply multiple e-mail addresses using a cell array of strings, such as:
sendmail({'recepient@someserver.com', ...
'recepient2@someserver.com'}, 'Hello From MATLAB!');
You can also specify a message body with the sendmail function, such as:
sendmail('recepient@someserver.com', 'Hello From MATLAB!', ...
'Thanks for using sendmail.');
In addition, you can also attach files to an e-mail using the sendmail function, such as:
sendmail('recepient@somesever.com', 'Hello from MATLAB!', ...
'Thanks for using sendmail.', 'C:\yourFileSystem\message.txt');
You cannot attach a file without including a message. However, the message can be empty. You can also attach multiple files to an e-mail with the sendmail function, such as:
sendmail('recepient@somesever.com', 'Hello from MATLAB!', ...
'Thanks for using sendmail.', ...
{'C:\yourFileSystem\message.txt',...
'C:\yourFileSystem\message2.txt'});
The following example sends e-mail with the retrieved Web page archive attached if it contains any matches for the specified word:
% The urlread function loads the contents of the Web page into
the % MATLAB workspace.
recentFile =
urlread('http://www.mathworks.com/matlabcentral/fileexchange/
loadFileList.do?objectType=fileexchange&orderBy=date&srt3=0');
% The strfind function searches for the word "Simulink".
hits = strfind(recentFile,'Simulink');
% The if statement checks for any hits.
if ~isempty(hits)
% If there are hits, the Web page will be saved locally
% using the urlwrite function.
urlwrite('http://www.mathworks.com/matlabcentral/fileexchange/
loadFileList.do?objectType=fileexchange&orderBy=date&srt3=0',
'contains_simulink.html');
% The zip function creates a zip archive of the retrieved web
page.
zip('simulink_matches.zip','contains_simulink.html');
% The setpref function supplies your e-mail address and SMTP
% server address to MATLAB.
setpref('Internet','SMTP_Server','mail.server.network');
setpref('Internet', 'E_mail', 'youraddress@yourserver.com');
% The sendmail function sends an e-mail with the zip archive of
the
% retrieved Web page attached.
sendmail('youraddress@yourserver.com', 'New Simulink Files
Found', 'New Simulink files uploaded to MATLAB Central. See
attached zip archive.', 'simulink_matches.zip');
end;
From MATLAB, you can connect to an FTP server to perform remote file operations. The following procedure uses a public MathWorks FTP server (ftp.mathworks.com). To perform any file operation on an FTP server, follow these steps:
Connect to the server using the ftp function.
For example, you can create an FTP object for the public MathWorks FTP server with tmw=ftp('ftp.mathworks.com').
Perform the file operations using appropriate MATLAB FTP functions as methods acting on the server object.
For example, you can display the file directories on the FTP server with dir(tmw).
When you finish working on the server, close the connection object using the close function.
For example, you can disconnect from the FTP server with close(tmw).
In this example, you retrieve the file pub/pentium/Moler_1.txt, which is on the MathWorks FTP server. You can run this example; the FTP server and content are valid.
Connect to the MathWorks FTP server using ftp. This creates the server object tmw:
tmw=ftp('ftp.mathworks.com');
List the contents of the server using the FTP dir function, which operates on the server object tmw:
dir(tmw)
Change to the pub directory by using the FTP cd function. As with all FTP functions, you need to specify the server object you created using tmw as part of the syntax. In this case, this is tmw:
cd(tmw,'pub');
The server object tmw represents the current directory on the FTP server, which now is pub.
dir(tmw)
you see the contents of pub, rather than the top level contents as displayed previously when you ran dir(tmw).
Use mget to retrieve any of the files from the current directory on the FTP server to the MATLAB current directory:
mget(tmw,'filename');
Close the FTP connection using close.
close(tmw);
The following table lists the available FTP functions. For more information, refer to the applicable reference pages.
Function | Description |
|---|---|
Set FTP transfer type to ASCII (convert new lines). | |
Set FTP transfer type to binary (transfer verbatim, default). | |
Change current directory on FTP server. | |
Delete file on FTP server. | |
List contents of directory on FTP server. | |
Close connection with FTP server. | |
Connect to FTP server, creating an FTP object. | |
Download file from FTP site. | |
Create new directory on FTP server. | |
Upload file or directory to FTP server. | |
Rename file on FTP server. | |
Remove directory on FTP server. |
![]() | Using Low-Level File I/O Functions | Scientific Data File Formats | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |