690 likes | 819 Views
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 = @sphere [XX YY ZZ] = feval(s,100). Example.
E N D
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 = @sphere [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) ? draw(@cylinder,100) ?
Cell arrays are similar to normal arrays … • but • They use curly brackets { } • They store elements of different types
x = {s 6} x = @sphere [6] x{1} ans = @sphere x{2} ans = 6
Graphic Objects • figure • axes • 2D-plot • 3D-plot • axis labels • title • GUI objects • pushbutton • toggle • edit • text • menu
Interface controllers • 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:
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 controllersare 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',... 'callback',{@closeCurrentFigure});
Interface controllersare 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
Checkboxes • Checkboxes allow the user to turn on/off a number of independent options hToCheckBox = uicontrol('Style', 'checkbox',... 'Position',[20 450 120 20],... 'String','Check Box example‘, ... ‘Callback’,’’) • Note: the position is measured in pixels from the left-bottom corner of the figure
Checkbox state • 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
Example function OnOffFrame uicontrol('style', 'toggle',... 'position', [50 100 150 200],... 'backgroundColor‘, 'b',... 'foregroundColor', 'r',... 'tag', 'onOff',... 'string', 'on',... 'FontSize', 40,... 'callBack', {@onOff},... 'userData', 1);
function OnOffFrame uicontrol('style', 'toggle',... 'position', [50 100 150 200],... 'backgroundColor‘, 'b',... 'foregroundColor', 'r',... 'tag', 'onOff',... 'string', 'on',... 'FontSize', 40,... 'callBack', {@onOff},... 'userData', 1); A handle to the function onOff Cell array
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 end
A matrix (in this case empty) A handle to the calling controller 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 end
Graphic objects • Figure • Axes
UI controllers • pushdown button • toggle button • radio buttons • text editor • text • slider
my_first_GUI – flow chart Initialize figure, UI controllers and menu
my_first_GUI – flow chart Initialize figure, UI controllers and menu UI 1 wait Activated? no yes Perform callback
my_first_GUI – flow chart Initialize figure, UI controllers and menu UI 1 wait Activated? no yes Perform callback UI 2 wait Activated? no yes Perform callback
my_first_GUI – flow chart Initialize figure, UI controllers and menu Main program UI 1 wait Get parameters from UI Activated? no yes Close figure yes Kill? Perform callback UI 2 no wait no display? Activated? no yes yes Calculate and update figure Perform callback
uimenu colormap figure my_first_GUI – flow of information slider uicontrol pushButton uicontrol edit uicontrol kill speed C radioButton uicontrol type function run (the drawing engine) On/off t C axes handles toggle uicontrol function draw (actually draws)
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,[1 2 3 4 5 6 ... 8 9 10 11 12 13 ... 15 16 17 18 19 20 ... 22 23 24 25 26 27 ... 29 30 31 32 33 34 ... 36 37 38 39 40 41]); 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
Buttons • Toggle buttons (or switches) are best suited for choosing between options like true/false, on/off and so on
Step III – the on/off button uicontrol('style' ,'toggle',... 'position' ,[0 10 60 30],... 'backgroundColor','b',... 'foregroundColor','r',... 'tag' ,'onOff',... 'string' ,'on',... 'callBack' ,{@onOff},... '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
Buttons • Push buttons are designed to launch a specific action like starting or stopping an execution, resetting the content of a form etc.
Step IV– the close button uicontrol('style' , 'pushbutton',... 'position' ,[65 10 60 30],... 'backgroundColor','b',... 'foregroundColor','r',... 'tag' ,'close',... 'string' ,'close',... 'callBack' , {@close_all},... 'userData' ,0); close on
function close_all(calling_button, eventData) set(calling_button,'userData',1); end on close
function close_all(calling_button, eventData) set(calling_button,'userData',1); end Why don’t we close the figure? on close
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 • Click the arrow buttons => small value changes • Click the trough => large value changes • Click and drag the slider => depends on user • The default changes are 1% and 10%
We must set a value between Min and Max 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 SliderStepproperty which is a two element vector ([0.01 0.1] default) hToVertSlider= uicontrol('Style','slider',... 'Position',[160 10 20 120],... 'Min',10,... 'Max',20,... 'Value',15,... 'SliderStep',[0.1 0.25],... 'Callback','')
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 = 15 + 0.1 * (20 - 10) • For a trough click: • Value = Value + SliderStep(2) * (Max – Min) • 17.5 = 15 + 0.25 * (20 - 10)