MATLAB: How to display an error if a file doesn’t exist

errorfileftp

I am working on a function that uses ftp to pull files from nasa's website. I want to make it so that if the file doesn't exist then display an error message. The following is the code of what is retrieved.
ftpobj = ftp('cddis.nasa.gov');
for i=1:days2get
dirStr = sprintf('/gnss/data/daily/2020/brdc/', doy(i));
fileStr = sprintf('BRDC00IGS_R_2020%03d0000_01D_MN.rnx.gz', doy(i));
cd(ftpobj, dirStr);
mget(ftpobj, fileStr, '.');
end
gunzip('*.gz');
Right now if the file doesn't exist I get an error saying that the file is not found on the server.

Best Answer

ftpobj = ftp('cddis.nasa.gov');
dirStr = sprintf('/gnss/data/daily/2020/brdc/';
cd(ftpobj, dirStr);
for i=1:days2get
fileStr = sprintf('BRDC00IGS_R_2020%03d0000_01D_MN.rnx.gz', doy(i));
d=dir(ftpobj,fileStr);
if isempty(d)
f = errordlg(['File: ' fileStr ' not found','File Error');
continue
end
mget(ftpobj, fileStr, '.');
end
gunzip('*.gz');
The dirStr has a what appears superfluous doy(i) after it -- didn't look like the site had additional structure; the files as listed were at the above directory.
Above will popup a dialog; you can do whatever instead -- the trick is to see if the dir() call is/is not empty. If the file exists, it'll be an ordinary-looking MATLAB dir() output struct.
Above worked when tried here...