A short intermission about function handles and cell arrays A MATLAB function may be referenced by a handle. >> sphere(100)
A short intermission about function handles and cell arrays A MATLAB function may be referenced by a handle. s [XX YY ZZ] = feval(s,100)
Example function draw(function_handle, function_parameter) [XX YY ZZ] = feval(function_handle, function_parameter); surf_handle = surf(XX,YY,ZZ); ax = get(surf_handle,'parent'); shading(ax,'INTERP') What would be the result of: draw(s,100) ? ?
Cell arrays are similar to normal arrays … but They use curly brackets { } They store elements of different types
x = {s 6} x [6] x{1} ans x{2} ans = 6
Continuing with GUI
Graphic Objects figure axes 2D-plot 3D-plot axis labels title GUI objects pushbutton toggle edit text menu
The UIcontrols are common interface controlers Help us perform specific actions or set the variables for future actions Actions and options are selected by the mouse, some UIcontrols are also editable so we can use the keyboard as well Interface controllers
We can create UIcontrols simply by implementing the following syntax handle_to_UI=uicontrol(‘Property Name’,’Property Value’);
Interface controllers UIcontrol types are defined by their ‘style’ property: Check box 'checkbox'Editable text field 'edit' Toggle button 'toggle' 'Pop-up menus 'popupmenu' Push button 'pushbuttonList box 'listbox' Radio button 'radiobutton' Static text 'text' Slider 'slider'Frame 'frame'
Essential properties: Callback – A string with one or more commands that is executed when the controller is activated. May be empty – ''
Example uicontrol('style','pushbutton','callback','close(gcf)');
Interface controllers are graphic objects Essential properties: Callback – A string with one or more commands that is executed when the controller is activated. May be empty - '' Recommendation – always call a function that does what you want.
function closeCurrentFigure(hCallingButton,... eventData) close(gcf) End uicontrol('style','pushbutton',...
Interface controllers are graphic objects Essential properties: Callback – A string with one or more commands that is executed when the controller is activated. May be empty - '' Recommendation – always call a function that does what you want. Tag – a string that may be used as a unique identifier of the controller. h = findobj('Tag','1');
Essential properties : Units – 'pixels' (default) or 'norm' (recommended) or Position – The vector [Left-lower-corner-X Left-lower-corner-Y width height] If you want to use the 'norm' units you should declare it before the position is set.
Essential properties : Value – a scalar String UserData – a matrix
hToCheckBox = uicontrol('Style', 'checkbox',... 'Position',[ ],... 'String','Check Box example‘,... ‘Callback’,’’) Checkboxes allow the user to turn on/off a number of independent options Note: the position is measured in pixels from the left-bottom corner of the figure Checkboxes
Represented by the Value property : 1 for checked 0 for unchecked >>state=get(hToCheckBox,’Value’) These values correspond to the Max and Min properties of the uicontrol which by default are 1 and 0 respectively Checkbox state
Example function OnOffFrame uicontrol('style', 'toggle',... 'position', [ ],... 'backgroundColor‘, 'b',... 'foregroundColor', 'r',... 'tag', 'onOff',... 'string', 'on',... 'FontSize', 40,... 'callBack', 'userData', 1);
function OnOffFrame uicontrol('style', 'toggle',... 'position', [ ],... 'backgroundColor‘, 'b',... 'foregroundColor', 'r',... 'tag', 'onOff',... 'string', 'on',... 'FontSize', 40,... 'callBack', 'userData', 1); Cell array A handle to the function onOff
function OnOffFrame uicontrol(: : :); function onOff(hCallingButton, eventData) state = get(hCallingButton,'userData'); if (state) %on set(hCallingButton,... 'string', 'off',... 'backgroundColor', 'r',... 'foregroundColor', 'b',... 'userData', 0); else set(hCallingButton,'string', 'on',... 'backgroundColor', 'b',... 'foregroundColor', 'r',... 'userData', 1); end end
function OnOffFrame uicontrol(: : :); function onOff(hCallingButton, eventData) state = get(hCallingButton,'userData'); if (state) %on set(hCallingButton,... 'string', 'off',... 'backgroundColor', 'r',... 'foregroundColor', 'b',... 'userData', 0); else set(hCallingButton,'string', 'on',... 'backgroundColor', 'b',... 'foregroundColor', 'r',... 'userData', 1); end end A handle to the calling controller A matrix (in this case empty)
Graphic objects Figure Axes
UI controllers pushdown button toggle button radio buttons text editor text slider
UI menu
Initialize figure, UI controllers and menu my_first_GUI – flow chart
Initialize figure, UI controllers and menu my_first_GUI – flow chart Activated? Perform callback yes no UI 1 wait
Initialize figure, UI controllers and menu my_first_GUI – flow chart Activated? Perform callback yes no UI 1 wait Activated? Perform callback yes no UI 2 wait
Initialize figure, UI controllers and menu my_first_GUI – flow chart Activated? Perform callback yes no UI 1 wait Activated? Perform callback yes no UI 2 wait Kill? display? Calculate and update figure yes no yes no Get parameters from UI Close figure Main program
function run (the drawing engine) function draw (actually draws) my_first_GUI – flow of information t C axes handles edit uicontrol C slider uicontrol speed toggle uicontrol On/off pushButton uicontrol kill uimenu figure colormap radioButton uicontrol type
Step I – setting the scene function my_first_gui draw_figure(600,600) end
Step I – setting the scene function my_first_gui draw_figure(600,600) end function draw_figure(width, height) screen_size = get(0,'screenSize'); left = screen_size(1)+5; bottom = screen_size(4)-height-40;
Step I – setting the scene function my_first_gui draw_figure(600,600) end function draw_figure(width, height) screen_size = get(0,'screenSize'); left = screen_size(1)+5; bottom = screen_size(4)-height-40; figure('position',[left bottom width height],... 'menuBar','none'); end
Step II – subplots function my_first_gui draw_figure('the_figure',600,600) main_axes = subplot(7,7,[ ]); small_axes = subplot(7,7,[7 14]); end function draw_figure(tag, width, height) screen_size = get(0,'screenSize'); left = screen_size(1)+5; bottom = screen_size(4)-height-40; figure('position',[left bottom width height],... 'tag', tag,... 'menuBar','none'); end
Toggle buttons (or switches) are best suited for choosing between options like true/false, on/off and so on Buttons
Step III – the on/off button uicontrol('style','toggle',... 'position',[ ],... 'backgroundColor','b',... 'foregroundColor','r',... 'tag','onOff',... 'string','on',... 'userData',1); on
Step III – the on/off button function onOff(calling_button, eventData) state = get(calling_button,'userData'); if (state) %on set(calling_button,'string','off',... 'backgroundColor','r',... 'foregroundColor','b',... 'userData',0); else set(calling_button,'string','on',... 'backgroundColor','b',... 'foregroundColor','r',... 'userData',1); end end on
function onOff(calling_button, eventData) state = get(calling_button,'userData'); if (state) %on set(calling_button,'string','off',... 'backgroundColor','r',... 'foregroundColor','b',... 'userData',0); else set(calling_button,'string','on',... 'backgroundColor','b',... 'foregroundColor','r',... 'userData',1); end end on
Push buttons are designed to launch a specific action like starting or stopping an execution, resetting the content of a form etc. Buttons
uicontrol('style', 'pushbutton',... 'position',[ ],... 'backgroundColor','b',... 'foregroundColor','r',... 'tag','close',... 'string','close',... 'callBack', 'userData',0); Step IV– the close button on close
function close_all(calling_button, eventData) set(calling_button,'userData',1); end onclose
function close_all(calling_button, eventData) set(calling_button,'userData',1); end onclose Why don’t we close the figure?
Sliders Sliders provide an easy way to gradually change values between a given range. Three ways to move the slider
Sliders The user has three possible way to change the position of the slider 1.Click the arrow buttons => small value changes 2.Click the trough => large value changes 3.Click and drag the slider => depends on user The default changes are 1% and 10%
Sliders The range of the slider is defined with the Min and Max uicontrol properties. The amount of change related to an user click is controlled with the SliderStep property which is a two element vector ([ ] default) hToVertSlider= uicontrol('Style','slider',... 'Position',[ ],... 'Min',10,... 'Max',20,... 'Value ',15,... 'SliderStep',[ ],... 'Callback','') We must set a value between Min and Max
Sliders Given the setting from Min, Max and SliderStep the amount of change to the current Value are as follow: For an arrow click: Value = Value + SliderStep(1) * (Max – Min) 16 = * ( ) For a trough click: Value = Value + SliderStep(2) * (Max – Min) 17.5 = * ( )
Step V– the speed slider uicontrol('style','slider',.... 'position',[ ],... 'tag','speedSlider',... 'max',pi/2,... 'min',0.01,... 'value',pi/4);
Step V– the speed slider uicontrol('style','slider',.... 'position',[ ],... 'tag','speedSlider',... 'max',pi/2,... 'min',0.01,... 'value',pi/4); What about the callback?
Text and editable text Static texts are commonly used to give instructions or to display other controllers values (such as sliders) …’Style’,’text’,… Static text can not execute callbacks. Editable texts gets string input from the GUI user. When the user enters the edit field and change its content, only the String property is affected. Editable text can execute callbacks
Step VI – the text frame uicontrol('style','text',... 'string','C',... 'tag','C_title',... 'position',[ ],... 'backgroundColor','y'); What about the callback?
Step VII – the edit window uicontrol('style','edit',... 'string','1',... 'value',1,... 'tag','C',... 'position',[ ],... 'callback', function getC(calling_button, eventData) s = get(calling_button,'string'); set(calling_button,'value',str2double(s)); end
Radio Buttons Radio buttons are similar to checkboxes but designed to specify options that are mutually exclusive like in multiple-choice questions hToRadio1 = uicontrol('Style', 'radio',... 'Position',[ ],... 'String','Option1',... 'Callback', turnOffTheOtherButtons},... 'Value',1) Selected
Function turnOffTheOtherButtons h = findobj('Tag','option1'); set(h,'Value',0); : Do we need a different function for each button?
Step VIII – the radio buttons uicontrol('style','radio',... 'string','surf',... 'callback', 'tag','plot_type',... 'position',[ ],... 'value',1,... 'userdata',1); uicontrol('style','radio',... 'string','contour3',... 'callback', 'tag','plot_type',... 'position',[ ],... 'value',0,... 'userdata',0);
Step VIII – the radio buttons function plotTypeButtons(calling_button, eventData) handles = findobj('tag','plot_type'); set(handles,'value',0); set(calling_button,'value',1); end
Step IX - Now lets use it function run(main_axes, small_axes) onOff = findobj('tag','onOff'); speedSlider = findobj('tag','speedSlider'); C_window = findobj('tag','C'); close_button = findobj('tag','close'); type_handels = findobj('tag','plot_type');
Step IX - Now lets use it function run(main_axes, small_axes) onOff = findobj('tag','onOff'); speedSlider = findobj('tag','speedSlider'); C_window = findobj('tag','C'); close_button = findobj('tag','close'); type_handles = findobj('tag','plot_type'); How does type_handles differ from the other handles?
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = ; end pause(0.1); end
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = ; end pause(0.1); end
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = ; end pause(0.1); end
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = ; end pause(0.1); end
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = ; end pause(0.1); end fig = findobj('tag','the_figure'); close(fig); end
cm = uimenu('tag','colorMap','label','Color Map'); Step XI – Adding color map menu
cm = uimenu('tag','colorMap','label','Color Map'); uimenu(cm, 'label','jet', uimenu(cm, 'label','hot', uimenu(cm, 'label','cool', Step XI – Adding color map menu function set_colorMap(calling_manu, eventData,colorMap) set(gcf,'colorMap',colorMap); parent = get(calling_manu,'parent'); all = get(parent,'children'); set(all,'checked','off'); set(calling_manu,'checked','on'); end
hToCheckBox = uicontrol('Style', 'checkbox',... 'Position',[ ],... 'String','Check Box example‘,... ‘Callback’,’’) Checkboxes allow the user to turn on/off a number of independent options Note: the position is measured in pixels from the left-bottom corner of the figure Checkboxes