Stop button (GUI)

조회 수: 3 (최근 30일)
Itziar Uzqueda
Itziar Uzqueda 2017년 2월 28일
댓글: Itziar Uzqueda 2017년 3월 1일
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

답변 (2개)

Image Analyst
Image Analyst 2017년 2월 28일
I have attached a demo for that.

Jan
Jan 2017년 2월 28일
편집: Jan 2017년 2월 28일
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
Itziar Uzqueda
Itziar Uzqueda 2017년 3월 1일
it's not working. It looks as if it wasn't changing the value os handles.stop. It never stops
Itziar Uzqueda
Itziar Uzqueda 2017년 3월 1일
Finally I have solved using global variables. Thank you

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by