Plotyy Function Error Graphing
2 views (last 30 days)
Show older comments
Can someone explain why I keep getting an error when I try to use the plotyy function? MatLab then tries to tell me to use the yyaxis function but I don't understand it and I'm supposed to use the plotyy function. Can someone explain where I keep messing up my code and why and how I can fix it. I'm supposed to have to seperate datas on one graph.


0 Comments
Answers (1)
sanidhyak
on 4 Jun 2025
Edited: sanidhyak
on 4 Jun 2025
I understand that you are encountering an error while using MATLAB’s "plotyy" function to graph two datasets on a single set of axes.
Upon reviewing your code and running a similar version, I also encountered the same issue. This error occurs because the "plotyy" function requires two sets of "Y-data" (and optionally "X-data"), whereas in your line:
plotyy(time, data_of_interest); % plot values of interest
only a single "Y-data" vector ("data_of_interest") is provided. This leads MATLAB to throw an error due to insufficient input arguments.
To resolve this, you need to pass two "Y-data" vectors (and two "X-data" vectors, if needed). Please refer to the following corrected code for the same:
% Assuming you want to compare two datasets on the same plot:
plotyy(time, data_of_interest, time, data_of_interest); % Example using the same data twice
If you are comparing different channels or segments, you can extract and plot them like this:
% First signal
channel1 = 1; segment1 = 1;
start1 = datastart(channel1, segment1);
stop1 = dataend(channel1, segment1);
data1 = data(start1:stop1);
rate1 = samplerate(channel1, segment1);
time1 = (0:length(data1)-1) / rate1;
% Second signal
channel2 = 2; segment2 = 3;
start2 = datastart(channel2, segment2);
stop2 = dataend(channel2, segment2);
data2 = data(start2:stop2);
rate2 = samplerate(channel2, segment2);
time2 = (0:length(data2)-1) / rate2;
% Truncate to common length if needed
min_len = min(length(data1), length(data2));
plotyy(time1(1:min_len), data1(1:min_len), time2(1:min_len), data2(1:min_len));
The command "plotyy(x1, y1, x2, y2)" is designed to plot two different datasets with separate "Y-axes". If only one dataset is provided, the function cannot proceed, resulting in the error which you faced.
For further reference on using "plotyy", please refer to the following official MATLAB documentation:
Cheers & Happy Coding!
0 Comments
See Also
Categories
Find more on Two y-axis 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!