MATLAB Answers

Continuous updating of GUI plotting

조회 수: 31(최근 30일)
JB
JB 28 Sep 2017
Commented: Adam 29 Sep 2017
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

표시 이전 댓글 수: 3
JB
JB 28 Sep 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 28 Sep 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
Adam
Adam 29 Sep 2017
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.

로그인 to comment.

답변 수 (0)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

태그

제품


Translated by