Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reusable Graphics Objects UC Berkeley Fall 2004, E77 me

Similar presentations


Presentation on theme: "Reusable Graphics Objects UC Berkeley Fall 2004, E77 me"— Presentation transcript:

1 Reusable Graphics Objects UC Berkeley Fall 2004, E77 http://jagger. me
Reusable Graphics Objects UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

2 Writing reusable graphical based applications
In order to write a reusable, graphical based application using Matlab HandleGraphics, the “application” should mimic the attributes of the basic Matlab HandleGraphics objects referred to by a handle get/set interface by which its properties (“state”) are accessed store hidden, globally accessible data have events associated with changes in its state This is relatively straightforward to do, but beyond the scope of the last 50 minute lecture of E77. Perhaps we need one last 18 page lab… By example, we partial overview of the strategy, minus the object oriented (user defined classes) wrapper.

3 Two more Matlab concepts
We need two more Matlab concepts before starting subfunctions, also called Local functions Cell array callbacks (as opposed to char callbacks last time)

4 Subfunctions In a function m-file, it is legal to have two (or more) functions. The first function is the main function The second, third, etc are subfunctions Each use the same syntax (original) for function declaration line Name of subfunctions usually begins with LOCAL The subfunctions are visible to The main function in the same file Any subfunctions in the same file Convenience of functions without extra .m files All variables in a subfunction are local Like functions, use input and output arguments to pass variables “in” and “out” of the subfunction. Subfunctions can called by their function_handle The main function can access the function handle and pass that back to its caller…

5 Subfunctions Here is a simple example subex.m >> M = 9;
>> [R,K] = subex(M,P); >> R R = 15 >> feval(K,2,8) ans = 4 function [A,H] = subex(B,C) A = B + LocalFcn1(B,C); H function Y = LocalFcn1(W,Q) Y = sqrt(W*Q); subex.m Every subfunction can be accessed anywhere through its handle.

6 Cell array callbacks Create (for example) a pushbutton
PB = uicontrol(’style’,’pushb’); and a function, ARG5.m and a 1x4 cell array cb = ’e77’ [100;10;1]}; Set the callback of the pushbutton to be the cell array. set(PB,’Callback’,cb); What happens when the button is pressed? function ARG5(A1,A2,A3,A4,A5) ... % some code here

7 Cell array callbacks Setup is PB = uicontrol(’style’,’pushb’);
cb = ’e77’ [100;10;1]}; set(PB,’Callback’,cb); Upon buttonpress, Matlab creates 2 variables, EventSrc and EventData, and executes an feval, without outputs feval(cb{1},EventSrc,EventData,cb{2:end}) equivalently ARG5(EventSrc,EventData,-14.1,’e77’,[100;10;1]) ARG5(A1, ,A ,A3 ,A4 ,A ) ARG5.m function ARG5(A1,A2,A3,A4,A5) ...

8 Cell array callback, common mistake
Setup is PB = uicontrol(’style’,’pushb’); V = 17 cb = ’e77’ V}; set(PB,’Callback’,cb); V = 18; Now press button. What will the value of the 5th argument to the function ARG5 be? Remember, Matlab will execute feval(cb{1},EventSrc,EventData,cb{2:end}) Clarify: not actually the variable cb, but the contents of pushbutton’s CallBack property, get(PB,’Callback’))

9 PushButton/Counter ++ ++ 1 ++ 7
Let’s make a reusable object consisting of a counter (initialized at 0) pushbutton (to increment counter) text display of counter value Initial appearance After one buttonpress After 7 buttonpresses User should also be able to program additional actions to occur after a button press. pushbutton frame text uicontrol ++ ++ 1 ++ 7

10 Overview of Tool Code function ToolH = fname(arg)
% Create objects which constitute the tool % These are the “subobjects” % Choose one subobject as the “tool handle” (ToolH) % Create ToolState struct which holds tool “state” % Hide ToolState in appdata of ToolH % Set subobject properties, event callbacks to subfcns % Input arg: ToolH (then subfcns can access ToolState) function subf1(Esrc,Edata,ToolH) % Ignore Esrc, Edata % Retrieve ToolState (from appdata of ToolH) % Do calcs, set properties of subobjects % Update and replace ToolState (appdata of ToolH) function subf2(Esrc,Edata,ToolH) function subf3(Esrc,Edata,ToolH)

11 PushButton/Counter State
Graphics object is the counter/display What information do we need to keep track of everything? value of counter handle of text display function_handle that user wants to additionally execute when the value is incremented Hence, tool state will be a structure with 3 fields CntVal, integer counter value DispHan, the handle of the text display uicontrol IncCB, function_handle (plus additional arguments) of user defined increment callback ++ 1

12 function ToolHan = e77gui1(X,Y,IncCB)
FrameH = uicontrol('style','frame',... 'position',[X Y ]); PlusH = uicontrol('style','pushbutton',... 'position',[X+5 Y ],'str','++'); DispH = uicontrol('style','text',... 'position',[X+50 Y ]); ToolHan = DispH; ToolState.DispHan = DispH; ToolState.CntVal = 0; ToolState.IncCB = IncCB; setappdata(ToolHan,'ToolState',ToolState); ToolHan}); set(DispH,'str',int2str(ToolState.CntVal)); function LOCALinc(ESrc, EData, TH) TS = getappdata(TH,'ToolState'); TS.CntVal = TS.CntVal + 1; set(TS.DispHan,'str',int2str(TS.CntVal)); setappdata(TH,'ToolState',TS); if ~isempty(TS.IncCB) feval(TS.IncCB{1},ESrc,EData,... TH,TS.IncCB{2:end}) end Create the objects that constitute the tool Designate toolhandle Create struct of Tool state (all relevant info) Hide state in ToolHan Set callback events & subobject properties Retrieve ToolState Do calculations, set subobject properties Update ToolState Run user callback if it exists, passing supplied arguments

13 function ToolHan = e77gui1(X,Y,IncCB)
FrameH = uicontrol('style','frame',... 'position',[X Y ]); PlusH = uicontrol('style','pushbutton',... 'position',[X+5 Y ],'str','++'); DispH = uicontrol('style','text',... 'position',[X+50 Y ]); ToolHan = DispH; ToolState.DispHan = DispH; ToolState.CntVal = 0; ToolState.IncCB = IncCB; setappdata(ToolHan,'ToolState',ToolState); ToolHan}); set(DispH,'str',int2str(ToolState.CntVal));

14 function ToolHan = e77gui1(IncCB)
% Omitted Code PlusH = uicontrol('style','pushbutton',... ToolHan}); function LOCALinc(ESrc, EData, TH) TS = getappdata(TH,'ToolState'); TS.CntVal = TS.CntVal + 1; set(TS.DispHan,'str',int2str(TS.CntVal)); setappdata(TH,'ToolState',TS); if ~isempty(TS.IncCB) feval(TS.IncCB{1},ESrc,EData,TH,... TS.IncCB{2:end}) end


Download ppt "Reusable Graphics Objects UC Berkeley Fall 2004, E77 me"

Similar presentations


Ads by Google