Plot data from 2 or more timeseries with different x axis scale

Hi everyone,
i have to plot 2 or more files as 1x1 timeseries from workspace. This is not the problem. The code looks like this:
figure
plot(simout.x1), hold on, grid on, plot(simout.x2),
xlim([0 31536000]), xlabel('time]'),ylabel('xxxx'),
title('xxxx'),legend('xx','xx'),
hold off
Because I simulate a year and have the data in seconds, I would like to have the X axis as months (Jan, Feb, Mar,...) in the plot of the whole year. Sometimes I would like to show only sections where the data in hours or days would be helpful.
Is there a way to implement this easily?
I have already tried some commands but could not do it. Would be glad about tips. Greetings

 Accepted Answer

If you supply to the plot command also x data - datetime class, then the plot will have the expected look by efault, and it will dynamically change the displayed units based on zoom.

5 Comments

Hi Jiri, thanks for your reply. Unfortunately it does not work. Either I insert it in the wrong places (tried everything) or it is because each record consists of a column with time steps and a column with the values of the variable and therefore the x values are not changed?
It is difficult to say what could be the problem on your side... So here is a simple example, which works for me, hopefully you can use it to check you implementation:
t1 = datetime(2015,1,1,8,0,0);
t2 = datetime(2015,12,20,8,0,0);
t=t1:hours(1):t2;
plot(t,rand(size(t)));
Hope this helps, please let me know..
I tried it with your code and it works fine. But with my data unfortunately not at all.
Let me show you again exactly what my data looks like. The data comes from Simulinnk with a "To Workspace" block as timeseries. The structure is in the picture.
The time goes to 31536000 seconds. And i want to plot one or minimum two of those in a figrue with the x axis in Months or hours.
Looks like the only thing to do is to convert your time coordinate into datetime. What you have in the Time field of your structure is relative time with respect to the start of your time series. To convert it into a datetime, you have to choose the absolute reference (e.g. 1.1.2020, 1:03:08,012) and then add your relative time converted into duration. I assume your relative time is "simout.Time" and your data for plotting is "simout.x1":
d = '2020-01-01 01:03:08.012';
t = datetime(d,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS');
timeVector = t+seconds(simout.Time);
plot(timeVector, simout.x1);
Very good. Thank you.
It works like this. I just had to add ".Data" to simout.x1. I then inserted the second plot with "hold on" and the same timeVector. Like this:
d = '2020-01-01 01:03:08.012';
t = datetime(d,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS');
timeVector = t+seconds(simout.x1.Time);
plot(timeVector, simout.x1.Data), hold on, plot(timeVector, simout.x2.Data)
Thank you very much.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Asked:

AF
on 17 Oct 2022

Edited:

AF
on 18 Oct 2022

Community Treasure Hunt

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

Start Hunting!