Programming Previous page   Next Page

Performing FTP File Operations

From within 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.
  1. For example, you can create an FTP object for the public MathWorks FTP server with tmw=ftp('ftp.mathworks.com').

  1. Perform the file operations using appropriate MATLAB FTP functions as methods acting on the server object.
  1. For example, you can display the file directories on the FTP server with dir(tmw).

  1. When you finish working on the server, close the connection object using the close function.
  1. 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:
  2. List the contents of the server using the dir FTP function, which operates on the server object tmw:
  3. Change to the pub/pentium directory by using the FTP cd function. As with all FTP functions, as part of the syntax you specify the server object you created using ftp, which in this case is tmw:
  1. The server object tmw represents the current directory on the FTP server, which now is pub/pentium.

  1. Now when you run
  1. you see the contents of pub/pentium, rather than the top level contents as displayed previously when you ran dir(tmw):

    • .
      ..
      Andy_Grove.txt
      Associated_Press.txt
      CNN.html
      Coe.txt
      ...
      Intel_replace.txt
      
    Intel_resp.txt
    Intel_support.txt
    Intel_white.ps
    MathWorks_press.txt
    Mathisen.txt
    Moler_1.txt

    Myths.txt
    Intel_resp.txt
    Intel_support.txt
    Intel_white.ps
    MathWorks_press.txt
    Mathisen.txt
    Moler_1.txt

    Myths.txt

  1. From the current directory on the FTP server, use mget to retrieve the file Moler_1.txt to the MATLAB current directory:
  2. Running a dir in MATLAB confirms that the file was downloaded.
  3. Close the FTP connection using close.

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
Change current directory on FTP server.
delete
Delete file on FTP server.
dir
List contents of directory on FTP server.
close
Close connection with FTP server.
ftp
Connect to FTP server, creating an FTP object.
mget
Download file from FTP site.
mkdir
Create new directory on FTP server.
mput
Upload file or directory to FTP server.
rename
Rename file on FTP server.
rmdir
Remove directory on FTP server.


Previous page  Sending E-Mail Error Handling Next page

© 1994-2005 The MathWorks, Inc.