Continuous updating of GUI plotting

I am preparing a gui where the visibility of plots are controlled by checkbox selection in two different axes. Additionally, the user should select the Y vector from a popupmenu. The code is working fine (it could be more elegant) but I have issues with refreshing the plot automatically. At present, If I plot and subsequently select a different Y values from the popupmenu I have to uncheck and recheck the checkmark for the changes to take place in the plot. How can I make the GUI refresh the plot automatically if it is selected (checkmark). Any help is much appreciated:
Here is my code:
% --- Executes on pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
X=[1 2 3 4]
Y1=[10 20 30 40]
Y2=[-1 -2 -3 -4]
handles.X=X;
handles.Y1=Y1;
handles.Y2=Y2;
guidata(gcbo, handles);
UnitFcn(handles)
% --- checkbox function on/off
function C = OnOffStr(D)
OffOn = {'off', 'on'};
L = (D ~= 0) + 1; % 0/FALSE => 1, anything else => 2
if length(L) == 1
C = OffOn{L}; % Reply a string
else
C = OffOn(L); % Reply a cell string
end
function UnitFcn(handles)
Y1=handles.Y1;
for p = 1:numel(plotdata)
Unit = get(handles.popupmenu1,'Value');
if (Unit==1)
Y(:,p)=Y1(:,p);
elseif (Unit==2)%
Y(:,p)=Y1(:,p)*100;
end
end
handles.Y=Y;
guidata(gcbo, handles);
PlotFcn(handles)
function PlotFcn(handles)
X=handles.X;
Y=handles.Y;
Z=handles.Y2;
%Plot in Axes 1
set(handles.axes1, 'NextPlot', 'add');
handles.plot1 = plot(X,Y,'visible','off','LineWidth',2, ...
'color', [0 0 0],'linestyle', '--', 'parent', handles.axes1);
%Plot in Axes 2
set(handles.axes2, 'NextPlot', 'add');
handles.plot2 = plot(X,Y2,'visible','off','LineWidth',2, ...
'color', [0 0 0],'linestyle', '--', 'parent', handles.axes2);
guidata(gcbo, handles);
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
set(handles.plot1, 'Visible', OnOffStr(get(hObject,'Value')));
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
set(handles.plot2, 'Visible', OnOffStr(get(hObject,'Value')));
% --- Specify unit in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
UnitFcn(handles)
function popupmenu1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
This is a simplified code and the "for p = 1:numel(plotdata)" refer to a matrix where I have ~30 different plots.

6 Commenti

JB - are you saying that the
function popupmenu1_Callback(hObject, eventdata, handles)
UnitFcn(handles)
is not being called when the user chooses something from the popup menu? Because this callback has the code to update the plot (I'm assuming that is what UnitFcn is doing...)
JB
JB il 28 Set 2017
Modificato: JB il 28 Set 2017
I can solve it if I include this code in the PlotFcn:
for i=1:1:numel(X(1,:)) Checked(i,1) = get(handles.(sprintf('checkbox%d',i)),'value'); if (Checked(i,1)==1) set(handles.plot(i), 'Visible', 'on') set(handles.plot(i), 'Visible', 'on') end end
but I am not sure this is the best solution?? It takes some time to run 30 plots with this code.
Adam
Adam il 28 Set 2017
As an aside: If you are continuously updating a plot it is far better to update the XData and YData of the same plotted object rather than keep plotting a new one.
JB
JB il 28 Set 2017
@Geoff. The UnitFcn is called appropriately and the X and Y values are recalculated according to the UnitFcn. But I have to uncheck and recheck the checkbox to make the new plot visible.
JB
JB il 28 Set 2017
@Adam. So the way the code is working now is the Y data is recalculated when the popupmenu is selected. Is this what you mean, or how can I change it within the XData and YData of the same plotted object. Thanks
handles.plot1
is the handle to the plot object you created. Instead of just keep replotting it you can use
handles.plot1.YData = ...
to update it with far better performance if you rework your code. Usually I do this by creating an empty plot handle initially then just using an if statement - if the handle is empty then do the plot instruction, else only update the YData (and any other properties that may have changed) of the existing plot.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Prodotti

Richiesto:

JB
il 28 Set 2017

Commentato:

il 29 Set 2017

Community Treasure Hunt

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

Start Hunting!

Translated by