Exchanging Files over the Internet

Overview

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.

Downloading Web Content and Files

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.

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.

Example — Using the urlread Function

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:

  1. 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');
  2. 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.

Example — Using the urlwrite Function

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.

Creating and Decompressing Zip Archives

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.

Example — Using the zip Function

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;

Sending E-Mail

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:

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.

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'});

Example — Using the sendmail Function

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;

Performing FTP File Operations

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:

  1. 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').

  2. 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).

  3. 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).

Example — Retrieving a File from an FTP Server

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.

  1. Connect to the MathWorks FTP server using ftp. This creates the server object tmw:

    tmw=ftp('ftp.mathworks.com');
    
  2. List the contents of the server using the FTP dir function, which operates on the server object tmw:

    dir(tmw)
    
  3. 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.

  4. Now when you run:

    dir(tmw)
    

    you see the contents of pub, rather than the top level contents as displayed previously when you ran dir(tmw).

  5. Use mget to retrieve any of the files from the current directory on the FTP server to the MATLAB current directory:

    mget(tmw,'filename');
    
  6. Close the FTP connection using close.

    close(tmw);

Summary of FTP Functions

The following table lists the available FTP functions. For more information, refer to the applicable reference pages.

Function

Description

ascii

Set FTP transfer type to ASCII (convert new lines).

binary

Set FTP transfer type to binary (transfer verbatim, default).

cd (ftp)

Change current directory on FTP server.

delete (ftp)

Delete file on FTP server.

dir (ftp)

List contents of directory on FTP server.

close (ftp)

Close connection with FTP server.

ftp

Connect to FTP server, creating an FTP object.

mget

Download file from FTP site.

mkdir (ftp)

Create new directory on FTP server.

mput

Upload file or directory to FTP server.

rename

Rename file on FTP server.

rmdir (ftp)

Remove directory on FTP server.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS