Monday, 13 January 2014

Matlab has a GUI tool?

So, I just found out that Matlab has a GUI creation tool, so the plus/minus tool I made in post one could have been done in Matlab, with a GUI front end. This would have been a lot quicker, but probably a lot less fun, and I wouldn't have added yet another language to my repertoire.

Either way I figured I'd have a play with the GUIde, so I tacked it on to my TimedWebcam function. This is a slight improvement over the Webcam.m function, in that it is only active between specified times.

Anyway, the GUI:

Open up Matlab, and enter "guide" (GUI de)

Create a new GUI with unicontrols

Create a simple Figure, 2 variable text boxes, 2 fixed ones, and a button:


Create the figure

Set the names and captions as you want them, as well as the starting values.

It will autocreate the basic script to generate and run the GUI, all you need to do is populate it.
In this case, all it does is feed the Start time <StT> and the Final time <FiT> to the preexisting function TimedWebcam (StT,FtT) when the main button is pressed, and keep the two variables updated if the values are changed.

The script is very similar to normal Matlab code, but because it is a series (parallel?) of individual functions, you can't pass variables directly. To do so we set a guidata object called handles which passes our data. to set a variable at the text box in this form we use:

handles.StT=StT;
guidata(hObject,handles)

and to retrieve it at the button we use

StT=handles.StT;

Pretty simple once you figure it out, and there are tutorials all over the web.

Full code:



function varargout = Webcam(varargin)
% WEBCAM M-file for Webcam.fig
%      WEBCAM, by itself, creates a new WEBCAM or raises the existing
%      singleton*.
%
%      H = WEBCAM returns the handle to a new WEBCAM or the handle to
%      the existing singleton*.
%
%      WEBCAM('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in WEBCAM.M with the given input arguments.
%
%      WEBCAM('Property','Value',...) creates a new WEBCAM or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Webcam_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Webcam_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 Webcam

% Last Modified by GUIDE v2.5 13-Jan-2014 08:07:39

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Webcam_OpeningFcn, ...
                   'gui_OutputFcn',  @Webcam_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 Webcam is made visible.
function Webcam_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 Webcam (see VARARGIN)

% Choose default command line output for Webcam
clc
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Webcam wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Webcam_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc
StT=handles.StT;
FiT=handles.FiT;
%fid=fopen(filename,'rt');
fprintf('Starting Webcam capture at %d:00, finishing at %d:00. \n Ctrl+C to break. \n \n ',StT,FiT)
TimedWebcam(StT,FiT);




function Commence_Callback(hObject, eventdata, handles)
% hObject    handle to Commence (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 Commence as text
%        str2double(get(hObject,'String')) returns contents of Commence as a double
StT = str2double(get(hObject,'String'));
handles.StT=StT;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function Commence_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Commence (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'),set(hObject,'string','9');
end
StT = str2double(get(hObject,'String'));
handles.StT=StT;
guidata(hObject,handles)



function Finalise_Callback(hObject, eventdata, handles)
% hObject    handle to Finalise (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 Finalise as text
%        str2double(get(hObject,'String')) returns contents of Finalise as a double
FiT = str2double(get(hObject,'String'));
handles.FiT=FiT;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function Finalise_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Finalise (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',set(hObject,'string','17'));
end
FiT = str2double(get(hObject,'String'));
handles.FiT=FiT;
guidata(hObject,handles)

No comments:

Post a Comment