MATLAB Answers

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

1 view (last 30 days)
Pai-Feng Teng
Pai-Feng Teng on 4 Jul 2021
Answered: Deepak Meena on 9 Jul 2021
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 Comments
Image Analyst
Image Analyst on 4 Jul 2021
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.

Sign in to comment.

Answers (1)

Deepak Meena
Deepak Meena on 9 Jul 2021
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).

Community Treasure Hunt

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

Start Hunting!

Translated by