%% 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, and mget 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')

%
% List the contents of the top-level folder on the FTP server.

dir(ftpobj)

%
% 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

%
% Change to the subfolder named pub on the FTP server.

cd(ftpobj,'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)

%
% 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.
% Copyright 2012 The MathWorks, Inc.