The only place in your data where I see moon phase is in column F of your spreadsheet and only the first month is indicated. There are lots of ways to denote the moon phase. This solution assumes that column F will contain this information for each month where each row in F will be one of the following:
- first quarter
- full moon
- last quarter
- new moon
All other rows will be ignored. Extra white space and case (upper/lower) will be ignored but if there are typos then the code will miss a phase. It would be very easy to change how you're denoting moon phase but I went with this since it's what I saw in your file.
Step 1: Read column F
You'll need to add column F to the data you're reading in
opts = spreadsheetImportOptions("NumVariables", 3);
opts.Sheet = "Edited data and charts";
opts.DataRange = "D2:F367";
opts.VariableNames = ["Date", "HighWater","MoonPhase"];
opts.VariableTypes = ["datetime", "double","char"];
Tides2020 = readtable("Tides2020_KSU.xlsx", opts, "UseExcel", false);
Step 2: get moon phase code and choose color
Here I add a new column to the Tides2020 data: MoonCode which is a value 1:4 or NaN. See inline comments below. If you decide to use a different indicator in column F of your spreadsheet, you'll need to make changes here.
Tides2020.MoonCode = nan(size(Tides2020.MoonPhase));
Tides2020.MoonCode(strcmpi('first quarter', strtrim(Tides2020.MoonPhase))) = 1;
Tides2020.MoonCode(strcmpi('full moon', strtrim(Tides2020.MoonPhase))) = 2;
Tides2020.MoonCode(strcmpi('last quarter', strtrim(Tides2020.MoonPhase))) = 3;
Tides2020.MoonCode(strcmpi('new moon', strtrim(Tides2020.MoonPhase))) = 4;
Now choose what color will be used for each of the phase (of course you can change the colors).
moonPhaseColor = [
0.85938 0.078125 0.23438;
0.0 0.5 0.0 ;
0.64453 0.16406 0.16406;
1.0 0.64453 0.0 ];
Step 3: Set colored bars indicating moon phase
There are a few ways to do this but the main idea is that you must create separate objects for each bar-color. The easiest and cleanest way to do that in this case (IMO) is to just overlay the colored bars on top of the other bars. This goes within your i-loop.
for i=1:12
subplot(6,2,i)
h=bar(Data(Start:End),'stacked');
hold on
for m = 1:4
tempX = h.XData(Tides2020.MoonCode(Start:End) == m);
tempY = h.YData(Tides2020.MoonCode(Start:End) == m);
tempH = bar(tempX,tempY);
tempH.FaceColor = moonPhaseColor(m,:);
end
Why are you using 'stacked' bars when you only have 1 column of data? You could removed 'stacked' and get the same results.
Since your excel file only contains moon phase data for 1 month, only January will contain the colored bars until you add the rest of the moon phases in column F of the spreadsheet.
Step 4: Make title 1 line
Here are two ways to solve this:
title(sprintf('%s %d', Months{i}, Arstall))
title([Months{i}, ' ', num2str(Arstall)]);