140123_Matlab GUI help Meg

advertisement
This is a helper for building GUIs in MATLAB
INTRODUCTION
Create the figure with the “uicontrols” (buttons, tables, plots) we want:
- Write “guide” to open the gui figure. Then put some components on it, and save it (remember to go to “Tools” in the
figure, click “GUI options” and define “Generate FIG file and MATLAB file”, so it also saves everything interactively in
a .m file with the same name as the figure).
Callbacks are what matter the most in the .m file:
-
In the .m file, only change code in the callbacks of the uicontrols.
Most important property of an uicontrol is the “Tag”, to define how to call it.
To get a property of the uicontrol, use “get”. To set a property, use “set”.
To get/set property of the uicontrol in the respective callback function, we get it from the handle “hObject”. Eg. In
a “edit box” that we can write, we get the written word with word_edit1=get(hObject,'String');
To get/set a property of another uicontrol in that function, we get it with “handles.Tag” (change object Tag)
Eg. In a “text box” text1 to display text of wordEdit1 we write set(handles.text1,'String',wordEdit1);
-
What to write in the OpeningFcn function:
-
-
Sometimes nothing needs to be written there, only on the callback functions
Definition of initial variables to be used in the GUI, eg. handles.word1='ze '
Definition of initial state of uicontrols, eg. set(handles.text1,'fontweight','bold')
Notes on saving and reopening gui file:
- Save the .fig and .m files with the same name
- To reopen the .fig file, type “guide”, then click on tab “Open existing Gui”, and select it
Notes on creating a gui from another gui:
-
-
Open the .fig file (type “guide”, then click on tab “Open existing Gui”, and select it)
Save the .fig file with a different name. It will produce a new .m file with the same name too.
TYPES OF UICONTROLS:
Very good tutorial (in Spanish) http://www.youtube.com/channel/UCSnDNJS65kPVVdjXi44-VnA

Edit text, Static text and Push Buttons:
Edit text are used to let the user insert data. Static text only displays data. Push buttons perform operations.
-
Static text has no callback function. The text is changed in the “String” property, eg:
set(handles.text1,'String','nice work');
-
Edit text has callback function. The text is inserted as the “String” property and is returned with ENTER, eg:
wordX=get(hObject,'String');
-
Push buttons have callback function for direct instructions, eg:
set(handles.text1,'String','nice work')

Pop-up menu, List box:
These two let the user choose among several options. They work exactly in the same way. They both have a callback
function.
-
To populate the possible choices by hand:
First write the possible choices on the “String” property (by hand, or with a line of code).
In the callback, get the content of “String” and the nr of the chosen one from “Value”, eg:
contentX=get(hObject,'String'); %get all possible choices
choiceNr=get(hObject,'Value'); %get the nr of the chosen one
set(handles.text2,'String',contentX(choiceNr)); %result displayed in static text
-
To populate the possible choices with one of the variables in the workspace, write in the callback:
vars = evalin('base','who') %get all workspace variable names
set(hObject,'String',vars); %populate the list (String) with the variable names
contents = cellstr(get(hObject,'String')); %pass all the vars names to contents
varName=contents{get(hObject,'Value')} %return the chosen variable name
set(handles.text2,'String', varName); %result displayed in static text
NOTE: for multiple selection, we have to include set(hObject,’max’,2,’min’,0)
And the difference between min and max must be >=2

Axes:
Axes are used to plot or draw figures. They do not have a callback function.
Axes are defined through GUI variables, eg. handles.axes1, for example in a pushbutton callback, eg:
function pushbutton1_Callback(hObject, eventdata, handles)
x=linspace(0,10,100)
axes(handles.axes1) % this line defines which axes to plot into
plot(sin(x))
NOTE: set(H_fig,'NumberTitle','off','Name','Testing Handle Graphics') deletes the nr of the figure

Radio buttons and Check boxes:
Used for selection of different pre-selected options. Radio buttons are exclusive (only one can be selected at a time) and
work as a group, check boxes are not exclusive and work as individual units. Both have callback function.
-
Radio buttons
First, in the figure, define a “button group” and inside of it create the number of buttons needed. Change their
“Strings” appropriately. Then, right-click in the “button group” and select “View Callbacks/SelectionChangeFcn”.
Then in the .m file, in the SelectionChangeFcn callback, get the string chosen by the user, and use “switch” and
“case” to perform actions, eg (to change the text size in a static text box to 10 or 14):
choiceX=get(hObject,'String')
switch choiceX
case '10'
set(handles.text1,'FontSize',10);
case '14'
set(handles.text1,'FontSize',14);
end
Note: last GUI I did, the example above did not work (there is no “String” property). I did it like this:
% in the GUI window OpeningFcn I defined:
handles.tailChoice='Both';
set(handles.uipanel2,'SelectedObject',handles.radiobuttonEqual)
% then on the callback I did:
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
switch get(hObject,'Tag')
case 'radiobuttonBoth'
handles.tailChoice='Both';
case 'radiobuttonRight'
handles.tailChoice='Right';
end
guidata(hObject, handles);
% finally I used the variable handles.tailChoice the way I wanted.
-
Check boxes
In checkboxes there is only selected or not selected (Value = 1 or 0). So, in the callback function, one only has to
get the “Value” property and perform something with it, by using for example an IF, eg:
checked=get(hObject,'Value');
if checked == 1
set(handles.text1,'ForegroundColor',[1,0,0]);
else
set(handles.text1,'ForegroundColor',[0,0,0]);
end

Sliders
Sliders are used to manually change the numerical properties of elements. They have a callback function.
In the following example, as the GUI writes the result from the slider to a static text box, the static text was initially
defined as 0 in the initializing function.
function sliderX_OpeningFcn(hObject, eventdata, handles, varargin)
set(handles.text1,'String', '0') % for initialization
function slider1_Callback(hObject, eventdata, handles)
valueX=get(hObject,'Value');
set(handles.text1,'String', valueX)

Tables
Tables are used to display, or insert data. They do not have a callback function.
First draw the table. Then right-click on it, select “Table property editor”. Here one can select the number of rows,
columns. There are multiple options, including opening a table from workspace or leave empty.
In this example, some data is defined in the initialization, and is put on the table through a pushbutton callback:
function tableX_OpeningFcn(hObject, eventdata, handles, varargin)
countries={'Portugal','Sweden','France'};
nrs={19,43,21};
dataX=[countries', nrs'];
set(handles.uitable1,'data',dataX);
function pushbutton1_Callback(hObject, eventdata, handles)
valuesX=get(handles.uitable1,'data')
% NOTE: STILL DON’T KNOW HOW TO INPUT VALUES ON TABLE BY HAND, OR BY COPY/PASTE

Message boxes (msgbox, errordlg, warndlg, helpdlg)
Used to display messages to the user. They don’t have callback function and all work in the same way, eg:
msgbox('That number is < 10','Well done!') % first the message, then box figure title
In the following example, different boxes are opened according to the input in an edit box:
function edit1_Callback(hObject, eventdata, handles)
nr=get(hObject,'String')
class(nr)
nr_X=str2double(nr)
class(nr_X)
if (nr_X) > 10
errordlg('Number is larger than 10','Error dude!')
set(handles.text1,'String','')
elseif (nr_X) == 10
warndlg('Number is equal to 10','Error dude!')
set(handles.text1,'String','')
elseif (nr_X<10)
set(handles.text1,'String',nr)
msgbox('Good work. That number is < 10','Well done!')
end
In the following example, a help dialog box is opened by pressing a push button:
function pushbutton1_Callback(hObject, eventdata, handles)
helpdlg('If the number is >10 you receive an error. If the number is equal to 10 you
receive a warning','Help for user dudes')

Menus and sub-menus
Menus are the top-left tabs “File, Save, etc”. They have callback functions.
First, on the figure, click in “Menu editor”. Then click “New Menu” to add a menu tab, change its name and tag. Then you
can add sub-menus to that one, by selecting it and clicking “New menu item”.
In the following example, a text file is open by clicking in a “Help” menu:
function Help_Callback(hObject, eventdata, handles)
winopen('help1.txt')

Input dialogs (do not have a callback)
Complete example of an input dialog to send variables from GUI to the workspace (by clicking on a button):
function pushbutton11_Callback(hObject, eventdata, handles)
figure, plot(handles.XalignedGoToWspace')
prompt={'Matrix with data:','Matrix with peak region limits'};
name='Save table to matrix in Workspace';
numlines=1;
defaultanswer={'IntResults','IntPeakLimits'};
answer=inputdlg(prompt,name,numlines,defaultanswer);
if strcmp(answer, 'Cancel')
return; % or break or whatever...
end
options.Resize='on';
options.WindowStyle='normal';
options.Interpreter='tex';
assignin('base',cell2mat(answer(1)),handles.areaPeaksAll);
assignin('base',cell2mat(answer(2)), handles.XalignedGoToWspace);
guidata(hObject,handles);
To use other functions (ex function f1) as helpers in our main gui function:
1. create a new function with a header like this:
function [ ]=f1();
2. define what I want inside the function. The variables that are going to be passed to the main gui function, are called, as
usually, handles.something.
3. In the main gui, insert this line in all callbacks that need this function f1:
f1(handles)
And don’t forget to update with : guidata(hObject, handles)
FILES AND VARIABLES
To open a file from disk:
[FILENAME, PATHNAME, FILTERINDEX] = uigetfile('*.mat')
Eg: for a menu File/Open, we write:
function Open_Callback(hObject, eventdata, handles)
[filenameX, pathnameX, filterindexX] = uigetfile('*.mat')
evalin('base',['load ' horzcat('(''',pathnameX,filenameX,''')')]);
guidata(hObject, handles); % updates handles
To save a file to disk: use uiputfile
Workspace to GUI interaction
-
To get the name of a variable from the workspace by opening an input dialog (varX will be a cell):
Eg. varX = inputdlg('input the variable name: ',window_title)
Eg. varX = cell2mat(inputdlg('Enter name of workspace variable',window_title));
% this will ask for the name of a variable (varX will be a string)
-
To load variable V1 from workspace to variable dataX in GUI: handles.dataX=evalin('base','V1')
-
To get the names of the variables in the workspace: baseVars = evalin('base','who')
-
To open an input dialog, write the name of a workspace variable, and load it in GUI:
VarName = cell2mat(inputdlg('Enter name of workspace var','import variable'))
handles.dataX=evalin('base',VarName)
guidata(hObject,handles) % this line updates the handles (to “global” GUI variables)
To write a variable varGUI (in the GUI) into the workspace:
Eg. assignin('base','name_var_in_workspace',varGUI)
To write a variable into excel sheet:
filename = 'testdata.xlsx';
A = {'Time','Temperature'; 12,98; 13,99; 14,97};
B = [23, 5, 34, 23, 21];
sheet = 1;
xlRange = 'A1';
xlswrite(filename,A,sheet,xlRange)
xlswrite(filename,B,sheet,'F1')
%We can add more “xlswrite” commands to add more things into the same excel sheet.
Pass local GUI var value in function callback to a global GUI variable (maybe defined in the initialization)
function pushbutton3_Callback(hObject, eventdata, handles)
handles.dataX=[1,2]
guidata(hObject,handles) % this updates all the handles (and the local variable to a global
one)
To plot a GUI variable by clicking on button :
function pushbutton3_Callback(hObject, eventdata, handles)
axes(handles.axes1) %call the desired axes1
plot(handles.dataX'), axis tight, title('dataX')
To plot a workspace variable by clicking on button:
handles.figX=figure
plot((evalin('base',handles.varName))');
title(evalin('base',handles.figX,handles.varName));
guidata(hObject, handles)
Interesting commands:
eval, evalin – to evaluate an expression in the workspace or another space
To plot in different colors:
colorsAvailable=hsv(size(x,1))
for a = 1:size(colorsAvailable,1)
plot(x(a,:), 'color',colorsAvailable(a,:))
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NOT ON GUIS, ONLY ON FIGURES
To create a listbox in a figure:
txt_cell_array = {'line1';'line2';'line3';'line4';'line5'};
h_list = uicontrol('style','list','max',10,'min',1,'Position',[20 20 80 60],'string',txt_cell_array);
To create a button in a figure:
h_button = uicontrol('style','pushbutton','String','Integrate NOW','Position',[0 0 100 30]);
Complete example of creation of a button and two edit boxes in a figure (from GUI):
First create the callback for the button:
function pushbutton13_Callback(hObject, eventdata, handles)
specValues=handles.valuesXpreproc;
handles.chosenSpecNr=get(handles.listbox1,'Value');% gets the nr of the selected spectra to plot
chosenSpecNames=(handles.allSpectra(handles.chosenSpecNr));% gets the names of the spectra to plot
(for legend)
handles.selSpecFig=figure('Name','Scatter plot');
handles.h_axis_scatter=plot(specValues(handles.chosenSpecNr,1),specValues(handles.chosenSpecNr,2),'.'
),grid;
texthandle1=text(specValues(handles.chosenSpecNr,1),specValues(handles.chosenSpecNr,2),num2str(chosen
SpecNames));
set(texthandle1,'FontSize',8);
uicontrol('Style','text','String','X axis:','Position',[1 2 40 20],'BackgroundColor',[0.8 0.8 0.8]);
handles.h_scatterX=uicontrol('Style','edit','String','1','Position',[41 5 60 20]);
uicontrol('Style','text','String','Y axis:','Position',[110 2 40 20],'BackgroundColor',[0.8 0.8
0.8]);
handles.h_scatterY=uicontrol('Style','edit','String','2','Position',[151 5 60 20]);
h_button = uicontrol('style','pushbutton','String','Change axis','Position',[231 3 80
25],'BackgroundColor',[0.8 0.8 0.8]);
set(h_button,'Callback',{@change_axis,handles})
Then create a help function to make it work:
function change_axis(hObject,eventdata,handles)
specValues=handles.valuesXpreproc;
handles.chosenSpecNr=get(handles.listbox1,'Value');% gets the nr of the selected spectra to plot
chosenSpecNames=(handles.allSpectra(handles.chosenSpecNr));% gets the names of the spectra to plot
(for legend)
int32(str2double(get(handles.h_scatterX,'String')))
handles.h_axis_scatter=axes;
current_x=int32(str2double(get(handles.h_scatterX,'String')));
current_y=int32(str2double(get(handles.h_scatterY,'String')));
clf
handles.h_axis_scatter=plot(specValues(handles.chosenSpecNr,current_x),specValues(handles.chosenSpecN
r,current_y),'.'),grid;
texthandle1=text(specValues(handles.chosenSpecNr,current_x),specValues(handles.chosenSpecNr,current_y
),num2str(chosenSpecNames));
set(texthandle1,'FontSize',8);
title('Plot any column variable against another')
uicontrol('Style','text','String','X axis:','Position',[1 2 40 20],'BackgroundColor',[0.8 0.8 0.8]);
handles.h_scatterX=uicontrol('Style','edit','String',num2str(current_x),'Position',[41 5 60 20]);
uicontrol('Style','text','String','Y axis:','Position',[110 2 40 20],'BackgroundColor',[0.8 0.8
0.8]);
handles.h_scatterY=uicontrol('Style','edit','String',num2str(current_y),'Position',[151 5 60 20]);
h_button = uicontrol('style','pushbutton','String','Change axis','Position',[231 3 80
25],'BackgroundColor',[0.8 0.8 0.8]);
set(h_button,'Callback',{@change_axis,handles})
Download