Clear Filters
Clear Filters

Two sets of data with two date ranges and y-axis values

3 views (last 30 days)
Hello all
I have two sets of data in Excel which I have uploaded to Matlab.
I want to plot these data on one grpah as a line/scatter graph.
Data Set 1
This has days on the x axis from day1 to day 250.
The value on the y axis goes up to 20 metres. I want to put this on the left side of the plot which is standard.
Data Set 2
This also has days on the x axis but the data only starts on day 15 and also goes up to 250.
The value on this y-axis for this data goes from 90 - 200 metres. I want to put this on the right side of the plot (Command = yyaxis right?)
I want to plot these two on the same plot but Data Set 2 values should only start on day 10 of Data Set 1
How do I get the data points to line up correctly?
Like this :
plot
code
hold on
code
I'm not sure if I have explained this correctly so please let me know if you need more of an explanation.
Many thanks

Accepted Answer

Star Strider
Star Strider on 6 Mar 2024
Edited: Star Strider on 6 Mar 2024
Try this —
x1 = 1:250;
y1 = 20*sin(2*pi*x1/100) + 20 + randn(size(x1))*5;
x2 = 15:250;
y2 = cos(2*pi*x2/150)*60 + 150 + randn(size(x2))*10;
figure
yyaxis left
plot(x1, y1)
ylabel('Y1')
yyaxis right
plot(x2, y2)
xl = xline(15,'--k', '‘(x2,y2)’ Start Time');
xl.LabelVerticalAlignment = 'bottom';
xl.LabelHorizontalAlignment = 'center';
ylabel('Y2')
grid
xlabel('X')
EDIT — (6 Mar 2024 at 19:02)
Added xline call to emphasize when ‘(x2,y2)’ begins.
.
  4 Comments
BenSven
BenSven on 6 Mar 2024
Thank you. I think I have figured it out.
Very much appreciated.

Sign in to comment.

More Answers (1)

Aquatris
Aquatris on 6 Mar 2024
Edited: Aquatris on 6 Mar 2024
Something like this maybe:
% create dummy time and data vectors
t1 = datetime(2013,11,1,8,0,0); % define random start of time vector
t2 = datetime(2014,11,1,8,0,0); % define random end of time vector
t_data1 = t1:t2; % time vector for data1
t_data2 = t_data1(120:end); % time vector for data2 that
% starts at a later date than time vector 1
data_1 = rand(length(t_data1),1)*20; % random data 1
data_2 = rand(length(t_data2),1)*200;% random data 2
% desired plot maybe?
yyaxis left
plot(t_data1,data_1,'b.') % plot data 1 with blue dots on left scale
ylabel('data 1')
yyaxis right
plot(t_data2,data_2,'r.') % plot data 2 with red dots on right scale
ylabel('data 2')
legend('data 1','data 2')

Community Treasure Hunt

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

Start Hunting!