How do I plot a timeseries?

조회 수: 100 (최근 30일)
Alexis
Alexis 2024년 2월 14일
편집: Adam Danz 2024년 2월 18일
I have data in an excel table. The first comlumn has time data (HH:MM.S) and the other 2 columns have wave height data. I need to plot the data with heights on the y-axis and the time on the x-axis. How do I go about this? I am mainly having trouble getting MATLAB to properly read the time data.

답변 (3개)

Star Strider
Star Strider 2024년 2월 14일
Use readtable. You can either use detectImportOptions to import the time as a datetime variable, or convert it after importing the file.

Hassaan
Hassaan 2024년 2월 14일
SInce I dont have your exact data file. Here is the initial approach you can try:
% Step 1: Import the Data
filename = 'your_file_name.xlsx'; % Replace 'your_file_name.xlsx' with your actual file name
data = readtable(filename);
% Step 2: Convert Time Data
% Assuming the time data is in the first column
% This part may need to be adjusted based on your specific time format
timeStrings = data.Var1; % Adjust 'Var1' if your variable name is different
% Preprocess and convert time strings to durations
% This assumes your time is in an unusual format and needs to be converted
% Example conversion (you may need to adjust this to fit your actual data format)
timeFormatted = strrep(timeStrings, '.', ':'); % Replace '.' with ':' to fit HH:MM:SS format
timeDurations = duration(timeFormatted, 'InputFormat', 'hh:mm:ss');
% Step 3: Plot the Data
% Assuming your wave height data is in the second and third columns
waveHeight1 = data.Var2; % Adjust 'Var2' if your variable name is different
waveHeight2 = data.Var3; % Adjust 'Var3' if your variable name is different
figure; % Create a new figure window
plot(timeDurations, waveHeight1, 'LineWidth', 1.5); % Plot first wave height dataset
hold on; % Hold on to plot the second dataset on the same figure
plot(timeDurations, waveHeight2, 'LineWidth', 1.5); % Plot second wave height dataset
hold off; % Release the plot
% Enhancing the plot with labels and legend
xlabel('Time');
ylabel('Wave Height (units)'); % Replace 'units' with your actual units of measurement
legend('Wave Height 1', 'Wave Height 2'); % Adjust the legend labels as necessary
title('Wave Height Over Time');
% Optionally, you can format the x-axis to display the time more clearly
ax = gca; % Get current axes
ax.XTickLabelRotation = 45; % Rotate the x-axis labels for better readability
Important Notes:
  • If your time data doesn't precisely fit the HH:MM.S format or MATLAB has trouble interpreting it, you might need to preprocess your time data in Excel or MATLAB to ensure it's in a recognizable time format.
  • The duration function and the InputFormat used in the conversion depend heavily on the exact format of your time data. If the format differs significantly from HH:MM.S, you'll need to adjust the InputFormat accordingly.
This should give you a good starting point. If your data or its format varies significantly from what's assumed here, you may need to tweak the steps accordingly.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Adam Danz
Adam Danz 2024년 2월 14일
편집: Adam Danz 2024년 2월 18일
Use readtimetable to create a timetable from your data file. If you're having problems with this step, provide a sample of your data including the headers.
After loading the timetable TT, plot the table variables using plot(TT,{'Var1','Var2'}) where Var1 and Var2 are your timetable variable names The x-values will be the timetable time stamps.
Here's a demo:
% Create timetable
MeasurementTime = datetime(2015,1,1)+days(0:5:600)';
Temp = 20+10*sin(linspace(0,10*pi,numel(MeasurementTime)))';
Pressure = randn(numel(MeasurementTime),1)*5+30;
WindSpeed = randn(numel(MeasurementTime),1)*10+10;
TT = timetable(MeasurementTime,Temp,Pressure,WindSpeed)
TT = 121×3 timetable
MeasurementTime Temp Pressure WindSpeed _______________ ______ ________ _________ 01-Jan-2015 20 28.426 7.3588 06-Jan-2015 22.588 23.303 5.9491 11-Jan-2015 25 36.502 27.73 16-Jan-2015 27.071 30.975 15.728 21-Jan-2015 28.66 26.652 -1.005 26-Jan-2015 29.659 31.224 -0.46994 31-Jan-2015 30 35.825 2.1641 05-Feb-2015 29.659 27.339 25.123 10-Feb-2015 28.66 27.85 17.184 15-Feb-2015 27.071 23.433 -2.2436 20-Feb-2015 25 30.963 27.34 25-Feb-2015 22.588 29.617 20.146 02-Mar-2015 20 30.544 -2.4134 07-Mar-2015 17.412 27.977 29.857 12-Mar-2015 15 38.619 21.15 17-Mar-2015 12.929 27.226 16.918
% Plot timetable, specify which table variables to plot
plot(TT,{'Temp','Pressure','WindSpeed'})
legend()

카테고리

Help CenterFile Exchange에서 Preprocessing Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by