Plotting Day and Time from csv file
13 views (last 30 days)
Show older comments
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')
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)');
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!