Stop button (GUI)

1 view (last 30 days)
Itziar Uzqueda
Itziar Uzqueda on 28 Feb 2017
Commented: Itziar Uzqueda on 1 Mar 2017
Hi,
I have this code below(except for a few beginning lines) and I have problems because I want the measurement to stop when I click on the Stop button, but it doesn't work. Any idea of the problem?
% --- 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)
% Choose default command line output for Medida_Serial_hcsr04
handles.output = hObject;
set(handles.guardar,'Enable','off');
handles.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');
%handles.stop=0;
cla reset; %Limpiar la grafica cada vez que se hace click
s = abrir_puerto();
handles.x=1:100;
handles.h=animatedline('Marker','o','LineWidth',1,'Color','b');
xlabel('Number of samples');
ylabel('Distance (cm)');
i=0; %Hay que recoger aqui el valor del que le pasa el stop button
while handles.stop==false
handles.distancia(i)=fscanf(s,'%d');
addpoints(handles.h,i,handles.distancia(i));
drawnow;
i=i+1;
end
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
handles.stop=true;
guidata(hObject,handles);
Thank you

Answers (2)

Image Analyst
Image Analyst on 28 Feb 2017
I have attached a demo for that.

Jan
Jan on 28 Feb 2017
Edited: Jan on 28 Feb 2017
You use the value of the handles struct, but do not update the struct:
while handles.stop==false
distancia(i)=fscanf(s,'%d');
addpoints(handles.h,i, distancia(i));
drawnow;
i=i+1;
handles = guidata(hObject); % <- get the current value
end
handles.distancia = distancia; % <- to avoid overwriting by GUIDATA
  2 Comments
Itziar Uzqueda
Itziar Uzqueda on 1 Mar 2017
it's not working. It looks as if it wasn't changing the value os handles.stop. It never stops
Itziar Uzqueda
Itziar Uzqueda on 1 Mar 2017
Finally I have solved using global variables. Thank you

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!