1 / 69

A short intermission about function handles and cell arrays

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.

jhurst
Télécharger la présentation

A short intermission about function handles and cell arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A short intermission about function handles and cell arrays A MATLAB function may be referenced by a handle. >> sphere(100)

  2. 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)

  3. 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) ?

  4. Cell arrays are similar to normal arrays … • but • They use curly brackets { } • They store elements of different types

  5. x = {s 6} x = @sphere [6] x{1} ans = @sphere x{2} ans = 6

  6. Continuing with GUI

  7. Graphic Objects • figure • axes • 2D-plot • 3D-plot • axis labels • title • GUI objects • pushbutton • toggle • edit • text • menu

  8. 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

  9. Interface controllers • We can create UIcontrols simply by implementing the following syntax • handle_to_UI=uicontrol(‘Property Name’,’Property Value’);

  10. Interface controllers • UIcontrol types are defined by their ‘style’ property:

  11. Essential properties: • Callback – A string with one or more commands that is executed when the controller is activated. • May be empty – ''

  12. Example uicontrol('style','pushbutton','callback','close(gcf)');

  13. 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.

  14. function closeCurrentFigure(hCallingButton,... eventData) close(gcf) End uicontrol('style','pushbutton',... 'callback',{@closeCurrentFigure});

  15. 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');

  16. 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.

  17. Essential properties : • Value – a scalar • String • UserData – a matrix

  18. 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

  19. 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

  20. 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);

  21. 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

  22. 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

  23. 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

  24. Graphic objects • Figure • Axes

  25. UI controllers • pushdown button • toggle button • radio buttons • text editor • text • slider

  26. UI menu

  27. my_first_GUI – flow chart Initialize figure, UI controllers and menu

  28. my_first_GUI – flow chart Initialize figure, UI controllers and menu UI 1 wait Activated? no yes Perform callback

  29. 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

  30. 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

  31. 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)

  32. Step I – setting the scene function my_first_gui draw_figure(600,600) end

  33. 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;

  34. 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

  35. 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

  36. Buttons • Toggle buttons (or switches) are best suited for choosing between options like true/false, on/off and so on

  37. 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

  38. 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

  39. 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

  40. Buttons • Push buttons are designed to launch a specific action like starting or stopping an execution, resetting the content of a form etc.

  41. 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

  42. function close_all(calling_button, eventData) set(calling_button,'userData',1); end on close

  43. function close_all(calling_button, eventData) set(calling_button,'userData',1); end Why don’t we close the figure? on close

  44. Sliders • Sliders provide an easy way to gradually change values between a given range. Three ways to move the slider

  45. 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%

  46. 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','')

  47. 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)

More Related