MATLAB: I need help with the GUI

matlab camsnapshot from camera matlab

I need some help with my following code:
(1) I my following code when push the button the start button the camera starts , while running I want to capture image from the cam after each 5 seconds and want to save that fram in a folder.
(2) when push stop button the cam should stop capturing frames too.
function varargout = MyFERApp(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MyFERApp_OpeningFcn, ...
'gui_OutputFcn', @MyFERApp_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 MyFERApp is made visible.
function MyFERApp_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 MyFERApp (see VARARGIN)
% Choose default command line output for MyFERApp
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes MyFERApp wait for user response (see UIRESUME)
% uiwait(handles.figure1);
handles.output = hObject;
imaqreset
handles.cam = webcam(1);
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = MyFERApp_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_Camera_button.
function Start_Camera_button_Callback(hObject, eventdata, handles)
% hObject handle to Start_Camera_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global KeepRunning;
KeepRunning=1;
while KeepRunning
cam = handles.cam;
data = snapshot(cam);
imshow(data, 'Parent', handles.axes1);
hold on
end
guidata(hObject, handles);
% --- Executes on button press in Stop_Camera_Button.
function Stop_Camera_Button_Callback(hObject, eventdata, handles)
% hObject handle to Stop_Camera_Button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global KeepRunning;
KeepRunning=0;
guidata(hObject, handles);
% --- Executes on button press in Delete_Camera_Button.
function Delete_Camera_Button_Callback(hObject, eventdata, handles)
% hObject handle to Delete_Camera_Button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cam = handles.cam;
delete (cam);
clear cam;
guidata(hObject, handles);

Best Answer

See the 'interruptible' property of callbacks in app designer - e.g. here under the heading 'Callback execution control'.
You need to create an opportunity for the Stop_Camera_Button_Callback to interrupt the while loop in Start_Camera_button_Callback. The sentence of interest is:
"The interruption occurs at the next point where MATLAB processes the queue, such as when there is a drawnow, figure, uifigure, getframe, waitfor, or pause command."
In other words a drawnow command can provide an oppurtunity for your stop button to interrupt the execution of the while loop to set the global variable, then when the while loop resumes KeepRunning will evaluate false (0).
Without drawnow, the stop button will wait until the previous callback is finished - which never occurs since your while loop will spin forever.
In your case you want the while loop to execute with a 5 second pause so the pause command might be more applicable.
ie
while KeepRunning
cam = handles.cam;
data = snapshot(cam);
imshow(data, 'Parent', handles.axes1);
hold on
pause(5) % Pause for 5 seconds and allow opportunity for Stop Button Callback to interrupt
end
Depending what you are after then the timer based implementation suggested by Geoff Hayes may be preferable.
Related Question