Using Nexttile or subplot in while and if loops ? Plotting issue
2 次查看(过去 30 天)
显示 更早的评论
Hello I am trying to run a MATLAB code that will produce plots for 50 years of water levels. I want the code to produce 10 years of water level plots for one decade 1971 to 1980, then another window for another decade 1981 - 1990 and so on untill 2020 one example of a window as follows
I can run a version of the script by changing the start years everytime to produce each windows plots, but since I am a beginner user of MATLAB i would want to dive more in the if loops so that the script runs automatically
The original water level is from the excel file attached and the data is extracted with a data processing m file script. The data processing one is for plotting the data. Part of the processing script is shown below But when i run the code it produces indivdual plots for the 50 years each in one window. I want the script to run so that it produces one window for the the 1st decade plots another window for the other decade and so one. The year start is 1971 till 2020.
I need some help with the if loops or any suggestions of modifications so that it run as stated above.
The code is as follows:
close all % Close pre-existing figures
n = 0; % doesnot need to be changed
year = 1971; %represents the start year of the loop
plotcount = 0 %doesnot need to be changed used to represent number of plots
span = [2 5] %represents rows and columns for each of figure windows ex 2 rows and 5 columns
while (n <= 50)
%Specify for how many years you want the plots after the start year
WinterStartdate = datetime("1971-01-01") + calyears(n); %%Change the year in the datetime to the desired start year
SummerStartdate = datetime("1971-07-01") + calyears(n); %%Change the year in the datetime to the desired start year
%% To explore the file: enter the desired range only the Winterstartdat and Summer Start Date years must be changed ALSO THE YEAR IN THE TITLE MUST BE CHANGED
tdays = (0:60); %represents 60 days from start to end date
Maxwaterlevel = max(water_level_15330); %finds the max waterlevel across all the years
WinterStartdatelocation =find(dates==(WinterStartdate), 1);%finds the location of the start date in the dates table
U = isempty(WinterStartdatelocation);
while U == 1 %checks whether the 1st exists within the array if not adds one day to the first day
WinterStartdate = WinterStartdate + caldays(1);
WinterStartdatelocation =find(dates==(WinterStartdate), 1);
U = isempty(WinterStartdatelocation);
end
WinterStartdatelocation = find(dates==(WinterStartdate));
WinterEnddatelocation = WinterStartdatelocation +60;%finds the location of the end date in the dates table
SummerStartdatelocation = find(dates==(SummerStartdate));%finds the location of the start date in the dates table
K = isempty(SummerStartdatelocation);
while K ==1 %checks whether the 1st exists within the array if not adds one day to the first day
SummerStartdate = SummerStartdate + caldays(1);
SummerStartdatelocation = find(dates==(SummerStartdate));
K = isempty(SummerStartdatelocation);
end
SummerEnddatelocation = SummerStartdatelocation + 60;%finds the location of the end date in the dates table
Winterwaterlevels = water_level_15330(WinterStartdatelocation:WinterEnddatelocation); %Creates an array with for the winter water levels from the start date to the end date
Summerwaterlevels = water_level_15330(SummerStartdatelocation:SummerEnddatelocation);%Creates an array with for the Summer water levels from the start date to the end date
WinterEnddate = WinterStartdate + caldays(60); %calculates the date after 60 days
SummerEnddate = SummerStartdate + caldays(60); %calculates the date after 60 days
Maxwinterlevel = max(Winterwaterlevels);
Maxsummerlevel = max(Summerwaterlevels);
if plotcount <10 %This for loop will plot on one figure for the first 10 years
nexttile(span)
plot(tdays,Winterwaterlevels,LineWidth=1,Marker="square",LineStyle="--",MarkerSize=5,Color="r"); %plots the 60 days v/s winter water levels
hold on ;
plot(tdays,Summerwaterlevels,LineWidth=1,Marker="^",MarkerSize=5,LineStyle="-",Color="b");
hold off;
title(num2str(year),FontSize=18); %changes the year to the current year analyzed
xlabel('No. of days from 1^{st} of month');
ylabel('Mean Water Level (m)');
ylim([0 2.5]); %the value to the right represents the maximum axis value on y axis
xticks(0:5:60) %1st number lower limit , middle number incrementations of ticks , last number represents upper limit on x axis
yticks(0:0.5:2.5) %1st number lower limit , middle number incrementations of ticks , last number represents upper limit on y axis
grid on;
n = n+1;
year = year +1
plotcount = plotcount+1
else
if plotcount >=10 %This for loop will plot on one figure for the second decade
nexttile(span)
plot(tdays,Winterwaterlevels,LineWidth=1,Marker="square",LineStyle="--",MarkerSize=5,Color="r"); %plots the 60 days v/s winter water levels
hold on ;
plot(tdays,Summerwaterlevels,LineWidth=1,Marker="^",MarkerSize=5,LineStyle="-",Color="b");
hold off;
title(num2str(year),FontSize=18); %changes the year to the current year analyzed
xlabel('No. of days from 1^{st} of month');
ylabel('Mean Water Level (m)');
ylim([0 2.5]); %the value to the right represents the maximum axis value on y axis
xticks(0:5:60) %1st number lower limit , middle number incrementations of ticks , last number represents upper limit on x axis
yticks(0:0.5:2.5) %1st number lower limit , middle number incrementations of ticks , last number represents upper limit on y axis
grid on;
n = n+1;
year = year +1
plotcount = plotcount+1
end
end
end
1 个评论
dpb
2022-11-4
Are the data all in one file? If so, a timetable and a little logic with grouping variables should do the trick quite easilty it would seem.
As always, it would be much easier to write clean code from the git-go with the definition of the input data than to try to clean up existing...
Best would be to attach the data file (if it's too big for the whole thing, just save 10% or so of the data by year) or attach a .mat file with the data array...
采纳的回答
Voss
2022-11-4
编辑:Voss
2022-11-4
close all % Close pre-existing figures
start_year = 1971; %represents the start year of the loop
span = [2 5] %represents rows and columns for each of figure windows ex 2 rows and 5 columns
tdays = 0:60; %represents 60 days from start to end date
Maxwaterlevel = max(water_level_15330); %finds the max waterlevel across all the years
for n = 0:49
%Specify for how many years you want the plots after the start year
WinterStartdate = datetime("1971-01-01") + calyears(n); %%Change the year in the datetime to the desired start year
SummerStartdate = datetime("1971-07-01") + calyears(n); %%Change the year in the datetime to the desired start year
%% To explore the file: enter the desired range only the Winterstartdat and Summer Start Date years must be changed ALSO THE YEAR IN THE TITLE MUST BE CHANGED
WinterStartdatelocation =find(dates==(WinterStartdate), 1);%finds the location of the start date in the dates table
while isempty(WinterStartdatelocation) %checks whether the 1st exists within the array if not adds one day to the first day
WinterStartdate = WinterStartdate + caldays(1);
WinterStartdatelocation =find(dates==(WinterStartdate), 1);
end
WinterStartdatelocation = find(dates==(WinterStartdate));
WinterEnddatelocation = WinterStartdatelocation +60;%finds the location of the end date in the dates table
SummerStartdatelocation = find(dates==(SummerStartdate));%finds the location of the start date in the dates table
while isempty(SummerStartdatelocation) %checks whether the 1st exists within the array if not adds one day to the first day
SummerStartdate = SummerStartdate + caldays(1);
SummerStartdatelocation = find(dates==(SummerStartdate));
end
SummerEnddatelocation = SummerStartdatelocation + 60;%finds the location of the end date in the dates table
Winterwaterlevels = water_level_15330(WinterStartdatelocation:WinterEnddatelocation); %Creates an array with for the winter water levels from the start date to the end date
Summerwaterlevels = water_level_15330(SummerStartdatelocation:SummerEnddatelocation); %Creates an array with for the Summer water levels from the start date to the end date
WinterEnddate = WinterStartdate + caldays(60); %calculates the date after 60 days
SummerEnddate = SummerStartdate + caldays(60); %calculates the date after 60 days
Maxwinterlevel = max(Winterwaterlevels);
Maxsummerlevel = max(Summerwaterlevels);
if mod(n,10) == 0
figure();
end
nexttile(span)
plot(tdays,Winterwaterlevels,LineWidth=1,Marker="square",LineStyle="--",MarkerSize=5,Color="r"); %plots the 60 days v/s winter water levels
hold on ;
plot(tdays,Summerwaterlevels,LineWidth=1,Marker="^",MarkerSize=5,LineStyle="-",Color="b");
hold off;
title(num2str(start_year+n),FontSize=18); %changes the year to the current year analyzed
xlabel('No. of days from 1^{st} of month');
ylabel('Mean Water Level (m)');
ylim([0 2.5]); %the value to the right represents the maximum axis value on y axis
xticks(0:5:60) %1st number lower limit , middle number incrementations of ticks , last number represents upper limit on x axis
yticks(0:0.5:2.5) %1st number lower limit , middle number incrementations of ticks , last number represents upper limit on y axis
grid on;
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!