Plot time on x axis for 24 hours duration
3 views (last 30 days)
Show older comments
Answers (1)
ag
on 26 Sep 2024
Hi NN,
To plot your data against time on the x-axis over a 24-hour duration, split every two hours, you can use MATLAB's datetime and plotting functions to handle time formatting and axis labeling.
The below code demonstrates how to achieve this using dummy data, along with explanatory comments:
% Sample data
data = rand(1, 13); % Replace with your actual data
% Create a time vector for 24 hours split every two hours
startTime = datetime('today', 'Format', 'HH:mm');
endTime = startTime + hours(24);
timeVector = startTime:hours(2):endTime;
% Ensure the data length matches the time vector length
if length(data) ~= length(timeVector)
error('Data length must match the number of time points.');
end
% Plot the data
figure;
plot(timeVector, data, '-o');
title('Data over 24 Hours');
xlabel('Time');
ylabel('Data Value');
grid on;
% Format the x-axis
ax = gca;
ax.XTick = timeVector; % Set x-ticks to the time vector
ax.XTickLabelRotation = 45; % Rotate x-tick labels for better readability
ax.XLim = [startTime, endTime]; % Set x-limits to the start and end time
For more details, please refer to the following MathWorks documentation: Represent Dates and Times in MATLAB- https://www.mathworks.com/help/matlab/matlab_prog/represent-date-and-times-in-MATLAB.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Annotations 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!