How do I plot a timeseries?
132 views (last 30 days)
Show older comments
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.
0 Comments
Answers (3)
Star Strider
on 14 Feb 2024
Use readtable. You can either use detectImportOptions to import the time as a datetime variable, or convert it after importing the file.
0 Comments
Hassaan
on 14 Feb 2024
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.
0 Comments
Adam Danz
on 14 Feb 2024
Edited: Adam Danz
on 18 Feb 2024
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)
% Plot timetable, specify which table variables to plot
plot(TT,{'Temp','Pressure','WindSpeed'})
legend()
0 Comments
See Also
Categories
Find more on Data Preprocessing 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!