Perform FTP File Operations
This example shows how to use an FTP object to connect to an FTP server and perform remote file operations. To perform any file operation on an FTP server, follow these steps:
Connect to the server using the
ftp
function.Perform operations using the appropriate MATLAB® FTP functions, such as the
cd
,dir
, andmget
functions. Specify the FTP object for all operations.When you finish work on the server, close the connection using the
close
function.
The National Centers for Environmental Information (NCEI) maintain an anonymous FTP service providing public access to geophysical data. Access the FTP server to list its contents, download a file, and list contents of a subfolder.
First, open the connection.
ftpobj = ftp('ftp.ngdc.noaa.gov')
FTP with properties: Host: "ftp.ngdc.noaa.gov" Username: "anonymous" Port: 21 ServerLocale: "en_US" DirParserFcn: @matlab.io.ftp.parseDirListingForUnix Mode: "binary" LocalDataConnectionMethod: "passive" RemoteWorkingDirectory: "/" CertificateFilename: "default" ConnectionTimeout: 5 min TransferTimeout: Inf sec
List the contents of the top-level folder on the FTP server.
dir(ftpobj)
DMSP INDEX.txt README.txt STP Snow_Ice Solid_Earth coastwatch dmsp4alan ftp.html geomag google12c4c939d7b90761.html index.html international ionosonde mgg pub wdc
Download the file named INDEX.txt
using the mget
function. mget
copies the file to the current MATLAB folder on your local machine. To view the contents of your copy of the file, use the type
function.
mget(ftpobj,'INDEX.txt'); type INDEX.txt
National Centers for Environmental Information (NCEI), formerly the National Geophysical Data Center (NGDC) INDEX of anonymous ftp area ftp.ngdc.noaa.gov DIRECTORY/FILE DESCRIPTION OF CONTENTS -------------- ----------------------------------------------------------------- pub/ Public access area DMSP/ Defense Meteorological Satellite Data Archive geomag/ Geomagnetism and geomagnetics models hazards/ Natural Hazards data, volcanoes, tsunamis, earthquakes international/ International program information on IAGA/Oersted/wdc ionosonde/ Ionosonde data mgg/ Limited Marine Geology and Geophysics (most data in http area) OD/ Office of the Director Snow_Ice/ Snow and Ice Data Center Solid_Earth/ Historic Solid Earth Geophysics STP/ Solar-Terrestrial Physics tmp/ Pickup area for temporary outgoing data wdc/ World Data Service for Geophysics, formerly World Data Centers -------------- ----------------------------------------------------------------- Please see file README.txt in this directory for more information and how to contact NCEI. Direct E-mail inquiries to ncei.info@noaa.gov Also see our web site: http://www.ngdc.noaa.gov/ NCEI is part of the: U.S. Department of Commerce, National Oceanic and Atmospheric Administration (NOAA), National Environmental Satellite, Data and Information Service (NESDIS)
Change to the subfolder named pub
on the FTP server.
cd(ftpobj,'pub')
ans = '/pub'
List the contents. pub
is now the current folder on the FTP server. However, note that the current MATLAB folder on your local machine has not changed. When you specify an FTP object using functions such as cd
and dir
, the operations take place on the FTP server, not your local machine.
dir(ftpobj)
WebCD coast glac_lib krm outgoing results rgon
Close the connection to the FTP server.
close(ftpobj)
FTP service courtesy of the NCEI. See the NCEI Privacy Policy, Disclaimer, and Copyright for NCEI terms of service.