MATLAB Answers

use a push button to close the GUI

조회 수: 494(최근 30일)
avram alter
avram alter 26 Sep 2019
댓글: Adam Danz 7 Oct 2019
I have a GUI that has a pushbutton that gets enabled once a certain condition has been met. I want to use that push button to then close the GUI.
here is my code:
function varargout = WorkingGUI3(varargin)
% WORKINGGUI3 MATLAB code for WorkingGUI3.fig
% WORKINGGUI3, by itself, creates a new WORKINGGUI3 or raises the existing
% singleton*.
%
% H = WORKINGGUI3 returns the handle to a new WORKINGGUI3 or the handle to
% the existing singleton*.
%
% WORKINGGUI3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WORKINGGUI3.M with the given input arguments.
%
% WORKINGGUI3('Property','Value',...) creates a new WORKINGGUI3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before WorkingGUI3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to WorkingGUI3_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help WorkingGUI3
% Last Modified by GUIDE v2.5 26-Sep-2019 17:02:03
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @WorkingGUI3_OpeningFcn, ...
'gui_OutputFcn', @WorkingGUI3_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before WorkingGUI3 is made visible.
function WorkingGUI3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to WorkingGUI3 (see VARARGIN)
handles.uipanels = [handles.uipanel1, handles.uipanel2];
set(handles.pushbutton5, 'enable', 'off');
% Choose default command line output for WorkingGUI3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes WorkingGUI3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end
% --- Outputs from this function are returned to the command line.
function varargout = WorkingGUI3_OutputFcn(hObject, eventdata, handles)
delete(instrfind('Port', 'COM3'));
tag = serial('COM3'); %check which port is used
fopen(tag);
BOX = char(zeros(2,14));
TrueValueData = 'C:\RfidChipTrueValues.xlsx';
[~,~,TrueValMat] = xlsread(TrueValueData);
% Creates matrix filled with the correct values,
% indexed by box, which is the first row
% all proceeding rows are the master value
for i=1:inf
for n = 1:2
if i>10
readData = fscanf(tag);
if length(readData)>12
BOX(str2num(readData(8)),1:14)= readData(11:24);
if strcmp(TrueValMat{2,n}, BOX(n,:))
set(handles.uipanels(n), 'BackgroundColor', 'g');
else
set(handles.uipanels(n), 'BackgroundColor', 'r');
end
drawnow
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) == 1
set(handles.pushbutton5, 'enable', 'on');
end
end
end
end
end
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
end
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
closereq();
end
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
end
everythig works fine, the button becomes enabled after my conditions are met, but pressing the button does nothing. I have also used closereq, close(handles.WorkingGUI3), delete(handles.WorkingGUI3). all help is appreciated.

  댓글 수: 0

로그인 to comment.

채택된 답변

Adam Danz
Adam Danz 26 Sep 2019
편집: Adam Danz 26 Sep 2019
Use the close request function
function pushbutton5_Callback(hObject, eventdata, handles)
closereq();
end
Only use gcf() or gca() when there is no other possible way to get the handle to a figure. For GUIDE GUIs, you can get the handle to the figure from the handles input (example: handles.figure1).

  댓글 수: 24

표시 이전 댓글 수: 21
Adam Danz
Adam Danz 7 Oct 2019
Hmmm.... I wonder if a drawnow() would work instead of the pause().
I didn't have that graphics problem when I ran your repaired GUI.
avram alter
avram alter 7 Oct 2019
I could try that, but I don't see any need to. I think the reason you aren't having the problem is that the incoming serial data is interfering. Maybe not. I don't know.
Adam Danz
Adam Danz 7 Oct 2019
Ahhhh..... I see. That's probably it. Thanks for the update!

로그인 to comment.

추가 답변(0개)

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


Translated by