The stop button doesn't terminate the process when pressed.

18 vues (au cours des 30 derniers jours)
Shubham Priyadarshan
Shubham Priyadarshan le 11 Avr 2023
Commenté : Rik le 11 Avr 2023
Hello all, below is my code of GUI. It was working fine but as soon as I added a slider to colloect the value of rate of data acquisition the orocess doesn;t terminate when stop button is pressed. The code is below: (Note the dataGen2 just returns a random number);
function varargout = testgui2(varargin)
% TESTGUI2 MATLAB code for testgui2.fig
% TESTGUI2, by itself, creates a new TESTGUI2 or raises the existing
% singleton*.
%
% H = TESTGUI2 returns the handle to a new TESTGUI2 or the handle to
% the existing singleton*.
%
% TESTGUI2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TESTGUI2.M with the given input arguments.
%
% TESTGUI2('Property','Value',...) creates a new TESTGUI2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before testgui2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to testgui2_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 testgui2
% Last Modified by GUIDE v2.5 11-Apr-2023 09:41:56
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @testgui2_OpeningFcn, ...
'gui_OutputFcn', @testgui2_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 testgui2 is made visible.
function testgui2_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 testgui2 (see VARARGIN)
% Choose default command line output for testgui2
handles.output = hObject;
handles.tdata = [];
handles.rate = get(handles.RateSlider,'value');
handles.stopval=0;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes testgui2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = testgui2_OutputFcn(hObject, eventdata, handles)
% 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;
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
i=1;
status1 = get(handles.start,'value');
status2 = get(handles.stop,'value');
while(status1 && ~status2)
rate3 = handles.rate;
value(i) = dataGen2;
time(i) = (i-1)/rate3;
handles.tdata = [time;value];
pause(1/rate3);
stem(handles.axes1,handles.tdata(1,:),handles.tdata(2,:));
i=i+1;
status2 = handles.stopval;
guidata(hObject,handles);
end
% --- 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)
handles.stopval=1;
guidata(hObject,handles);
% --- Executes on button press in save.
function save_Callback(hObject, eventdata, handles)
% hObject handle to save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
data = handles.tdata';
headers = {'time', 'value'};
output = [headers; num2cell(data)];
writetable(cell2table(output), 'data.txt', 'Delimiter', 'tab', 'WriteVariableNames', false);
function rate_Callback(hObject, eventdata, handles)
% hObject handle to rate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of rate as text
% str2double(get(hObject,'String')) returns contents of rate as a double
% --- Executes during object creation, after setting all properties.
function rate_CreateFcn(hObject, eventdata, handles)
% hObject handle to rate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on slider movement.
function RateSlider_Callback(hObject, eventdata, handles)
% hObject handle to RateSlider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
sliderMin = get(hObject, 'Min');
sliderMax = get(hObject, 'Max');
% Get the current value of the slider
sliderValue = get(hObject, 'Value');
% Scale the slider value to the appropriate range
rate2 = sliderMin + (sliderValue*(sliderMax - sliderMin));
handles.rate = rate2;
guidata(hObject,handles);
% Update the rate display
% --- Executes during object creation, after setting all properties.
function RateSlider_CreateFcn(hObject, eventdata, handles)
% hObject handle to RateSlider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

Réponses (1)

Adam Danz
Adam Danz le 11 Avr 2023
Looks like
status2 = handles.stopval
should be
status2 = handles.stopval.Value;
  1 commentaire
Rik
Rik le 11 Avr 2023
Just to be clear: this is true both in the while loop, and in the callback.
You should make sure to set the stop button back when you start the function, otherwise you can only use the stop once.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by