要打开到 FTP 服务器的连接,请创建 FTP 对象。使用 FTP 对象下载文件并列出服务器上子文件夹的内容。在 FTP 会话结束后,关闭连接。
首先,连接到美国国家环境信息中心 (NCEI) 的 FTP 服务器。
ftpobj = ftp('ftp.ngdc.noaa.gov')
ftpobj =
FTP Object
host: ftp.ngdc.noaa.gov
user: anonymous
dir: /
mode: binary
列出顶层文件夹的内容。
dir(ftpobj)
DMSP Solid_Earth google12c4c939d7b90761.html mgg
INDEX.txt coastwatch hazards pub
README.txt dmsp4alan index.html tmp
STP ftp.html international wdc
Snow_Ice geomag ionosonde
从 FTP 服务器下载 README.txt 文件。mget 函数会下载一个副本到您当前的 MATLAB® 文件夹中。
mget(ftpobj,'README.txt');
使用 fileread 函数读取 README.txt 副本的内容。使用 splitlines 函数将文本分成几行,并显示前四行。
readme = fileread('README.txt');
readme = splitlines(readme);
readme(1:4)
ans = 4×1 cell array
{' Welcome to