Plot time on x axis for 24 hours duration

3 次查看(过去 30 天)
NN
NN 2020-11-21
回答: ag 2024-9-26
How can i plot my data against time on x axis which is 24 hours duration split every two hours.
Sample has been attached here.

回答(1 个)

ag
ag 2024-9-26
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!

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by