Continuous updating of GUI plotting

3 次查看(过去 30 天)
JB
JB 2017-9-28
评论: Adam 2017-9-29
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 个评论
JB
JB 2017-9-28
@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
Adam
Adam 2017-9-29
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by