issue when creating and saving file to specific folder

7 次查看(过去 30 天)
Hello, everyone. I got a small issue when creating a folder and saving files to it.
-------------------------------------------------------------------
monthStr = num2str(month);
mkdir(fullfile(folder,'Data',monthStr));
dataFolder = fullfile(folder,'Data',monthStr);
dataFolder
cd dataFolder ;
-----------------------------------------------------------------
Here I create a folder /Data/monthStr, where monthStr is a variable, for instance, it can be 202106
it is created under /folder/Data and 'dataFolder' shows its path correctly.
However, when I "cd dataFolder", error message says "Cannot CD to .... (Name is nonexistent or not a directory)."
Secondly, I like to save some downloaded file to this folder
--------------------------------------------------------------------
fullURL=['https://polar.ncep.noaa.gov/waves/hindcasts/multi_1/' monthStr '/gribs/multi_1.glo_30m.hs.' monthStr '.grb2';];
filename=[dataFolder,'multi_1.glo_30m.hs.' monthStr '.grb2'];
[f,status]=urlwrite(fullURL,filename);
--------------------------------------------------------------------
However, the file is saved to the "Data" folder, not "Data/monthStr" folder
Any idea to correct the code?
Thank you!

回答(2 个)

Rik
Rik 2021-6-17
You shouldn't use cd outside of debugging. In general using a full path is a much better idea with better performance.
The cause of the error is that you used the command syntax, which doesn't allow the use of variables. In this case you would have needed to use cd(dataFolder).
For the second issue you forgot to add the filesep, which fullfile would have done automatically for you.
  5 个评论
Rik
Rik 2021-6-17
Because there is a difference between Windows and Linux/Mac which character separates folders and files. You can automate this like this:
['folder' filesep 'subfolder' filesep 'file']
which is equivalent to this
fullfile('folder','subfolder','file')
shukui liu
shukui liu 2021-6-17
Noted!
I am very new with Matlab...
The language is very flexible, really like a language, very different from the old style.
Many thanks for the prompt response!

请先登录,再进行评论。


Chunru
Chunru 2021-6-17
When dataFolder is a variable, change directory with the following statement:
cd(dataFolder) ;
  3 个评论
Stephen23
Stephen23 2021-6-17
编辑:Stephen23 2021-6-17
Avoid using CD in code: it is slow (MATLAB rescans the new folder for all MATLAB files and potentially caches them), changes function scope (which makes debugging harder) and is completely unnecessary.
Using absolute/relative filenames is the recommended approach (much more efficient, less buggy).
shukui liu
shukui liu 2021-6-17
Thanks, Stephen.
I am working based on previous version code, which reads data from ftp server. Now the data has been shifted to web server.
In previous version, there is such a "filesMask" to get the list of the files on ftp server and then assign their name to "files"? That is what I understand.
Currently, I will download these files directly from web server. Then, to get the "files", I need to go to the folder (cd) and then use the original code. I thought this might be the easiest way to update the code.
Actually, I am not very clear with the two expressions
filesMask = ['multi_1.' type '.*.grb2'];
files = f.dir(filesMask);
I shall appreciate much if you can shed some light on it.
ftpSite = 'polar.ncep.noaa.gov';
ftpPath = ['/pub/history/waves/multi_1/' monthStr '/gribs'];
% Connect to the FTP site
f = ftp(ftpSite);
f.cd(ftpPath);
% Get the list of relevant *.grb2 files
filesMask = ['multi_1.' type '.*.grb2'];
files = f.dir(filesMask);
% If no matching files found, issue an error
if isempty(files)
f.close(); % Close the FTP connection
url = ['ftp://' ftpSite, ftpPath];
error('No %s files were found in <a href="%s">%s/</a>', filesMask, url, url)
end
% If fields were specified, then exclude the irrelevant files
if ~isempty(fields)
regex = ['\.(' fieldsRegex ')\.'];
invalidIdx = cellfun('isempty', regexp({files.name},regex));
files(invalidIdx) = [];
end
% Download all files into the Data folder
if ~exist(dataFolder,'dir')
fprintf('Downloading %d files into %s ...\n', length(files), dataFolder);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by