plot several functions on multiple GUI axes.

Hi, i'm trying to plot several functions on an GUI axes. Some over the rigth axis and some others on the left axis. Rigth and left axis are diferent cause i want to compare signal that have so different amplitude i know how to do it on a script but it dosn't work on a GUI on Matlab2015. On later versions is as easy as use yyaxis function. It dosn´t works on 2015b
I wan´t to see both axis rigth and left at the same time.
thank you in advance

3 Comments

Angel - can you show a picture of what you want? It isn't clear to me what you mean by I wan´t to see both axis rigth and left at the same time. Have you added two (or more) axes objects to your GUI? Or do you just have one? Please clarify.
Hi! thank you for your answer. This is wath i want:
Captura.JPG
I know how to do it in one shot. The problem is when i try to add signals to one axis or the other after an event on a list. When i try to add a new signal to one axis, the other axis vanish and the graph auto scale to the new signal so you can´t see properly all the datas.
thank you in advance.
After 2016b release is as easy as use yyaxis left or yyaxis rigth to select the axis where you want to plot.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 22 Nov 2018
Edited: Cris LaPierre on 22 Nov 2018
yyaxis was not introduced until r2016a. You need to use plotyy. Visit the linked documentation page for examples of how to use it.

7 Comments

I Cris. Thak you for your answer. Plotyy is not usefull in my case. I want to plot more than 2 signals at the same time. And if i try to pass by handles the axis references (AX) obtained with [AX H1 H2]=plotyy(...); it doesn´t work as i explain in the question before.
I need to perform the same as yyaxis left or rigth does but in matlab 2015b
Thank you
You can plot more than 2 signals. Did you see the example in the plotyy documentation for plotting two data sets on the right y-axis?
[hAx,hLine1,hLine2] = plotyy(x,y1,[x',x'],[y2',y3']);
Here, y1 is on the left, and y2 and y3 are on the right. This can be expanded to work for any number of data sets on the left or right y-axis.
Note that, in that example, x, y2 and y3 had to be turned into column vectors and were concatenated to appear as a single input. They therefore do have to be the same length.
You can also use the axis handle to add more plots to an existing plotyy plot. This format allows you to add data sets with different lengths. Here is an example of how to create the same plot from the example using hold and plot to add the third line.
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
hold(hAx(2),'on')
plot(hAx(2),x,y3)
hold(hAx(2),'off')
thanks Cris, The last option you propose i what i´m trying to do but when i try to pass the axes created during the first call of a callback function to a call before it doesn´t work. I´ll try ploting all the signals every tiem the function is call. Is not the best way but maybe is the only one.
kind regards
Could you share some code? If you copy and paste the code above, it works. Without seeing what you are doing different, I can't offer specific suggestions to you.
One thought is that in a gui, callbacks are functions. You must consider variable scope (variables created within a function are not saved once the function is done running).
If you are in app designer, add the variables you want to reuse as properties (just the name is fine). This adds them to the app structure, and they can be referred to using app.hAx(1), etc.
properties (Access = private)
hAx; % Handle to axes
hLine1; % Handle to lin
...
end
% Button pushed function: Button
function ButtonPushed(app, event)
[app.hAx,app.hLine1,app.hLine2] = plotyy(app.UIAxes,x,y1,x,y2);
hold(app.hAx(2),'on')
plot(app.hAx(2),x,y3)
hold(app.hAx(2),'off')
end
If you are using guide, the similar concept is achieved by adding your variables to the handles structure. To update the structure, you would need to add guidata(hObject, handles); to the end of a function that has modified the handles structure.
[handles.hAx,handles.hLine1,handles.hLine2] = plotyy(handle.axes1,x,y1,x,y2);
hold(handles.hAx(2),'on')
plot(handles.hAx(2),x,y3)
hold(handles.hAx(2),'off')
% Update handles structure
guidata(hObject, handles);
Hi Cris,
i want to populate the axes with signals selected from a list. Something like the following:
At the opening function i try to set some axes:
Then when i try to use them i ´m not successfull:
Itry different codes but no one works properly.
thank you in advance
I did leave one thing out in my examples - specifying which axes to plot in as part of the plot() syntax. I have updated it now.
I've created a simple example in GUIDE based on the code I've shared previously that is working. You are welcome to pursue the approach you are taking above - the principles are likely the same.
% --- Executes just before updatePlot is made visible.
function updatePlot_OpeningFcn(hObject, eventdata, handles, varargin)
% ...
% Choose default command line output for updatePlot
handles.output = hObject;
[handles.ax,handles.L1,handles.L2] = plotyy(handles.axes1,0:180,zeros(181,1),0:180,zeros(181,1));
% left axis
xlim(handles.ax(1),[0 180]);
ylim(handles.ax(1),[-0.1 1.2]);
yticks(handles.ax(1),'auto');
hold(handles.ax(1),'off')
% right axis
ylim(handles.ax(2),[0 240]);
yticks(handles.ax(2),'auto');
hold(handles.ax(2),'off')
% Update handles structure
guidata(hObject, handles);
I'm not sure what behavior you want, or how many plots you want to display in the end (more than 2, though). For the code to work, it appears to be important that you create the original plotyy with vectors of data. I originally tried using [...]=plotyy(0,0,0,0) and [...]=plotyy([0 0],[0 0],[0 0],[0 0]) and it does not work correctly.
I also specifically turn hold off and use an if statement to have the first plot replace the zeros plot used in creating the original plotyy graph and then turn hold on. Here is my listbox callback.
% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% ...
file = get(handles.listbox1,'Value');
signal = get(hObject,'Value');
time = 0:180;
switch signal
case 1
selected = sin(time-pi/2)*randi(file);
case 2
selected = cos(time)*50*randi(file);
case 3
selected = sin(time)*randi(file);
case 4
selected = cos(time+pi/2)*50*randi(file);
end
if signal == 1 || signal == 3
if ishold(handles.ax(1))
plot(handles.ax(1),time,selected);
else
set(handles.L1,'XData',time,'YData',selected);
hold(handles.ax(1),'on');
end
elseif signal == 2 || signal == 4
if ishold(handles.ax(2))
plot(handles.ax(2),time,selected);
else
set(handles.L2,'XData',time,'YData',selected);
hold(handles.ax(2),'on');
end
end
Again, this is loosely based on what you displayed above to help you see how to adapt it. I don't modify the handles structure here, so there is no need to add guidata(hObject, handles); to the code. On the first time through (when ishold is false), I just update the XData and YData properties of the existing line and turn hold on for that axis. Every other time, I add a new line to the specified axis.
Hope this helps.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Release

R2015b

Asked:

on 22 Nov 2018

Edited:

on 27 Nov 2018

Community Treasure Hunt

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

Start Hunting!