Plotting Day and Time from csv file

13 views (last 30 days)
I am attempting to plot hourly power data from the first day of each month for all 24 hours of that day for one year (288 date/time points). Ideally, it should be grouped into twelve curves, one curve for each day. I extracted the date-time information from a .csv file and plotted my output power data against these date-time values. Unfortunately, MATLAB automatically filled in all the days in between the first day of each month with zeros on my graph, so I have twelve sharp, unreadable peaks instead of twelve curves where I can read the data at each hour.
The code I used to plot is attached, as well as the hourlyTempWindData.csv, Q_Total is a 1x288 double that's calculated by the rest of my code (which is very long), so I've included a random number array instead. I want my graph to look like the second one (one day with a data point at each hour) instead of the first (unreadable peak with flat space in between).
Also, is there anything I can do to stop the orange warning from showing up after I use readtable to get the data from the csv? It isn't interfering with my code running, it's just annoying.
%Import radiation and weather data sheets, and set variables for each
%column, then transpose values to a row vector
hourlyData = readtable("hourlyTempWindData.csv", 'VariableNamingRule','preserve')
hourlyData = 288×4 table
DATE HourlyDryBulbTemperature HourlyWindSpeed HourlyWindSpeed ft/s ___________________ ________________________ _______________ ____________________ 2019-01-01T00:54:00 10 21 30.8 2019-01-01T01:54:00 9 22 32.267 2019-01-01T02:54:00 7 20 29.333 2019-01-01T03:54:00 7 15 22 2019-01-01T04:54:00 6 18 26.4 2019-01-01T05:54:00 5 18 26.4 2019-01-01T06:54:00 5 21 30.8 2019-01-01T07:54:00 5 15 22 2019-01-01T08:54:00 5 14 20.533 2019-01-01T09:54:00 6 13 19.067 2019-01-01T10:54:00 6 11 16.133 2019-01-01T11:54:00 7 10 14.667 2019-01-01T12:54:00 10 10 14.667 2019-01-01T13:54:00 12 5 7.3333 2019-01-01T14:54:00 14 6 8.8 2019-01-01T15:54:00 15 3 4.4
dates = datetime(table2array(hourlyData(:,1)));
Q_Total = randi(50, [1 288]);
%Graph daily energy losses/gains against date and hour, results in 12 sharp
%peaks with flat space between
figure(1)
hold off
grid on
plot(dates, Q_Total)
title('Hourly Energy Needs for Greenhouse');
xlabel('Date and Time');
ylabel('Q (Btu/h)');
%Graph daily energy losses/gains, results in 12 curves (when using actual
%data, not randi)
figure(2)
hold off
grid on
plot(Q_Total);
title('Hourly Energy Needs for Greenhouse');
xlabel('Date and Time');
ylabel('Q (Btu/h)');

Accepted Answer

Cris LaPierre
Cris LaPierre on 7 Apr 2023
With your data as datetimes, there is no good way to crop out the missing days, and workarounds I might try are cumbersome. Since the ideal is to have all 12 days plotted as their own line, I'll do that.
hourlyData = readtable("hourlyTempWindData.csv", 'VariableNamingRule','preserve');
% add power, month (for grouping) and
hourlyData.Q_Total = randi(50, [288,1]);
hourlyData.Month = month(hourlyData.DATE);
hourlyData.Hour = hour(hourlyData.DATE);
G = findgroups(hourlyData.Month);
figure
hold on
splitapply(@(x,y) plot(x,y),hourlyData(:,["Hour","Q_Total"]),G)
hold off
ylabel('Power')
xlabel('Hour of Day')
legend(num2str(unique(hourlyData.Month)))
  6 Comments
Aussia S.
Aussia S. on 8 Apr 2023
My ultimate vision was to lable each point with its respective time, but I understand this would get messy very quickly, as you noted. Can the xticks be changed so each peak is labeled with its respective month? Perhaps centering the xtick where the peak of each day is (or at least close to that general area. Thank you for your help!
Cris LaPierre
Cris LaPierre on 8 Apr 2023
Yes. Use the same format as what I've shared. The only additions are
  • Find each peak (options include max, findpeaks, or Find Local Extrema live task)
  • Extract the index of each peak (returned by the functions I mentioned)
  • Use xticks to set the tick locations to the desired values
There rest is already there for you.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!