Programming |
|
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:
- 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)
.
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.
- Connect to the MathWorks FTP server using
ftp
. This creates the server object tmw
:
- List the contents of the server using the dir FTP function, which operates on the server object
tmw
:
- 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
:
- The server object
tmw
represents the current directory on the FTP server, which now is pub/pentium.
- Now when you run
- you see the contents of pub/pentium, rather than the top level contents as displayed previously when you ran
dir(tmw)
:
|
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
|
- From the current directory on the FTP server, use mget to retrieve the file
Moler_1.txt
to the MATLAB current directory:
- Running a
dir
in MATLAB confirms that the file was downloaded.
- 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.
|
| Sending E-Mail | | Error Handling | |
© 1994-2005 The MathWorks, Inc.