Main Content

mget

Download files from SFTP or FTP server

Description

mget(s,contents) retrieves the files or folders specified by contents from the SFTP or FTP server associated with s into the MATLAB® current folder.

example

mget(s,contents,target) retrieves the files or folders into the local folder specified by the absolute or relative path in target. If the local folder does not exist, mget creates it.

example

downloadPaths = mget(___) also returns the paths to the downloaded files and folders as a cell array of character vectors. You can use the input arguments from either of the previous syntaxes.

Examples

collapse all

Download a text file from an FTP server and display its contents.

First, connect to the National Centers for Environmental Information (NCEI) FTP server.

s = 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

Download a text file. The mget function downloads the file to the current folder on your machine.

mget(s,'README.txt');

Display the beginning of README.txt. To read the copy of README.txt downloaded to your computer, use the fileread function.

readme = fileread('README.txt');
readme(1:95)
ans = 
    '                 Welcome to the 
         NOAA/National Centers for Environmental Information (NCEI)'

FTP service courtesy of the National Centers for Environmental Information (NCEI). See the NCEI Privacy Policy, Disclaimer, and Copyright for NCEI terms of service.

Download a text file from an FTP server to a specified folder on your local machine.

First, connect to the National Centers for Environmental Information (NCEI) FTP server.

s = 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

Download a text file to a folder named myLocalFolder. If this folder does not exist, then the mget function creates it on your local machine.

mget(s,'README.txt','myLocalFolder');

Read the beginning of README.txt using the fileread function.

readme = fileread('myLocalFolder/README.txt');
readme(1:95)
ans = 
    '                 Welcome to the 
         NOAA/National Centers for Environmental Information (NCEI)'

FTP service courtesy of the National Centers for Environmental Information (NCEI). See the NCEI Privacy Policy, Disclaimer, and Copyright for NCEI terms of service.

Input Arguments

collapse all

Connection to an SFTP or FTP server, specified as an SFTP object or an FTP object.

Remote files or folders, specified as a character vector or string scalar.

To match multiple files or folders on the SFTP or FTP server, you can include a wildcard character (*) in contents. For example, if you specify contents as *.docx, then mget downloads all files whose names end with .docx.

Local folder, specified as a character vector or string scalar. target can specify a relative or absolute path.

Version History

Introduced before R2006a

See Also

| | |