the code that works manually but don't work at all with loop

1 次查看(过去 30 天)
I tried to export 131 time series regarding weather data for 20 years in 6 folders. I have to create a loop. When I tested it by entering the code manually and the code worked. However the loop didn't when I want to run it.
I have double-checked input data many times. Yet it still didn't work after I run the code
The following code is my loop
input = {'atem';'clou';'dewp';'pevt';'prec';'solr';'wind'};
for k = 2:length(input)
mydir6 = ['C:\Users\Victor\Documents\PhD\2021Summer\HUC12_EALSTM\Data\trial4_RCH131_S\weather\' char(input(k))];
mkdir(mydir6);
for i = 1:length(a_HUC12)
string6 = [z_InputFormat,ColS]; %duplicate the input format files
string6(2,1)= [char(string6(2,1)) as]; %input the aditional variable
for j = 1:length(a_dates)
string6(j+2,2:end) = 1; % this experiment will simply make weather input other than storage as 1
string6(j+2,k+1) = Inputfulls(j,i,k); %only input storage
filename6 = strcat(string6(:,1),tab,string6(:,2),tab,string6(:,3),tab,string6(:,4),tab,string6(:,5),tab,string6(:,6),tab,string6(:,7),tab,string6(:,8),tab,string6(:,9));
filename6(2,1) = ans2;
filename6(1,1) = round(a_area(i));
end
dir6 = [mydir6 '\' num2str(a_HUC12(i)) '_weather.txt'];
writematrix(filename6, dir6); %export them to the txt
end
end
input = {'atem';'clou';'dewp';'pevt';'prec';'solr';'wind'};
for k = 2:length(input)
mydir6 = ['C:\Users\Victor\Documents\PhD\2021Summer\HUC12_EALSTM\Data\trial4_RCH131_S\weather\' char(input(k))];
mkdir(mydir6);
for i = 1:length(a_HUC12)
string6 = [z_InputFormat,ColS]; %duplicate the input format files
string6(2,1)= [char(string6(2,1)) as];
for j = 1:length(a_dates)
string6(j+2,2:end) = 1; % this experiment will simply make weather input other than storage as 1
string6(j+2,k+1) = Inputfulls(j,i,k); %only input storage
filename6 = strcat(string6(:,1),tab,string6(:,2),tab,string6(:,3),tab,string6(:,4),tab,string6(:,5),tab,string6(:,6),tab,string6(:,7),tab,string6(:,8),tab,string6(:,9));
filename6(2,1) = ans2;
filename6(1,1) = round(a_area(i));
end
dir6 = [mydir6 '\' num2str(a_HUC12(i)) '_weather.txt'];
writematrix(filename6, dir6); %export them to the txt
end
end
  3 个评论
dpb
dpb 2021-7-4
编辑:dpb 2021-7-4
rootdir='C:\Users\Victor\Documents\PhD\2021Summer\HUC12_EALSTM\Data\trial4_RCH131_S\weather\';
for k = 2:length(input)
mydir6=fullfile(rootdir, input{k});
if exist(mydir6)==0
mkdir(mydir6);
end
...
is the most rudimentary way to not try to recreate an existing folder; it could have significantly more error-checking than this, but at least it won't try to recreate an existing folder when you run the code more than the first time.
Image Analyst
Image Analyst 2021-7-4
if ~isfolder(mydir6)
mkdir(mydir6);
end
@Pai-Feng Teng, don't use input as the name of your variable since it's the name of a built-in function. Call it something else, like fileTypes or fileFormats or something.

请先登录,再进行评论。

回答(1 个)

Deepak Meena
Deepak Meena 2021-7-9
Hi,
I tried to reproduce the error but you haven't mentioned the values such as a_dates , a_HUC12.
Also mention the error message you are getting while running the code.
For now I agree with @Image Analyst and @dpb that you should add the check whether the same directory exists or not (but it will just throw the warning not error).

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by