Problems when re-running matlab gui after closing GUI with exit button

조회 수: 4 (최근 30일)
Itziar Uzqueda
Itziar Uzqueda 2017년 3월 1일
답변: Image Analyst 2017년 3월 1일
Hi,
I have this code below. My problem is that when I close the Matlab Gui using the exit button in the window (the one similar to an 'x') the next time I try to run matlab, it doesn't work until y press Ctrl+C to stop the execution. I think it is because i'm using Serial communications and the port hasn't been closed, but i'm not completely sure. So, if anyone could help me, I would be very grateful.
function varargout = Medida_Serial_hcsr04(varargin)
% MEDIDA_SERIAL_HCSR04 MATLAB code for Medida_Serial_hcsr04.fig
% MEDIDA_SERIAL_HCSR04, by itself, creates a new MEDIDA_SERIAL_HCSR04 or raises the existing
% singleton*.
%
% H = MEDIDA_SERIAL_HCSR04 returns the handle to a new MEDIDA_SERIAL_HCSR04 or the handle to
% the existing singleton*.
%
% MEDIDA_SERIAL_HCSR04('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MEDIDA_SERIAL_HCSR04.M with the given input arguments.
%
% MEDIDA_SERIAL_HCSR04('Property','Value',...) creates a new MEDIDA_SERIAL_HCSR04 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Medida_Serial_hcsr04_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Medida_Serial_hcsr04_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 Medida_Serial_hcsr04
% Last Modified by GUIDE v2.5 01-Mar-2017 10:33:50
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Medida_Serial_hcsr04_OpeningFcn, ...
'gui_OutputFcn', @Medida_Serial_hcsr04_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 Medida_Serial_hcsr04 is made visible.
function Medida_Serial_hcsr04_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 Medida_Serial_hcsr04 (see VARARGIN)
clear s;
% Choose default command line output for Medida_Serial_hcsr04
handles.output = hObject;
set(handles.guardar,'Enable','off');
global stop;
stop = false;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Medida_Serial_hcsr04 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Medida_Serial_hcsr04_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in Medir.
function Medir_Callback(hObject, eventdata, handles)
% hObject handle to Medir (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.Abrir,'Enable','off');
global stop
stop=false;
cla reset; %Limpiar la grafica cada vez que se hace click
s = abrir_puerto();
handles.h=animatedline('Marker','o','LineWidth',1,'Color','b');
guidata(hObject,handles);
xlabel('Number of samples','FontSize',16,'FontWeight','bold','Color','w');
ylabel('Distance (cm)','FontSize',16,'FontWeight','bold','Color','w');
set(gca, 'XColor', [1 1 1]);
set(gca, 'YColor', [1 1 1]);
i=1;
while stop==false
handles.distancia(i)=fscanf(s,'%d');
addpoints(handles.h,i-1,handles.distancia(i));
drawnow;
i=i+1;
end
stop=false;
set(handles.guardar,'Enable','on');
set(handles.Abrir,'Enable','on');
guidata(hObject,handles);
cerrrar_puerto(s);
% --- Executes on button press in guardar.
function guardar_Callback(hObject, eventdata, handles)
% hObject handle to guardar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dist = handles.distancia;
guardar_archivo(dist);
% --- Executes on button press in Abrir.
function Abrir_Callback(hObject, eventdata, handles)
% hObject handle to Abrir (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Abrir_fichero();
% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject handle to stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of stop
global stop
stop=true;
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
delete(hObject);
Thank you

답변 (1개)

Image Analyst
Image Analyst 2017년 3월 1일
Call fclose() when you shut down the app.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by