plot several functions on multiple GUI axes.
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Cris LaPierre
il 22 Nov 2018
Modificato: Cris LaPierre
il 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 Commenti
Cris LaPierre
il 27 Nov 2018
Modificato: Cris LaPierre
il 27 Nov 2018
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.
Vedere anche
Categorie
Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



