HandleGraphics Intro UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
A graphical user interface (GUI) is a pictorial interface to a program. A good GUI can make programs easier to use by providing them with a consistent.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
MATLAB and Simulinklecture 31 To days Outline  Functions  Strings  Sparse Arrays  Cell Arrays  Structures  Exercises on this days topics.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Review of C++ Programming Part II Sheng-Fang Huang.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
Programming a GUI Hanan sedaghat pisheh. For calling GUI, we need a function with no inputs or outputs First We create a m.file m file has the same name.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Program Guide v25Q3. Overview » Concepts » Workflow  Press sheet  Linking product  Program guide  Publishing a program guide day » Layout configuration.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Fall 2006AE6382 Design Computing1 OOP: Creating a Class More OOP concepts An example that creates a ASSET class and shows how it might be used Extend the.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
A short intermission about function handles and cell arrays A MATLAB function may be referenced by a handle. >> sphere(100)
Introduction to the Graphical User Interface (GUI) in MATLAB
Matlab GUIs GUIDE.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab.
Introduction to Matlab & Data Analysis
Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
Simulink SubSystems and Masking April 22, Copyright , Andy Packard. This work is licensed under the.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
MATLAB and SimulinkLecture 51 To days Outline Linear Equations ODE Integration Handle Graphics. Exercises on this days topics.
SE: CHAPTER 7 Writing The Program
Introduction to Matlab’s Graphical User Interface (GUI) Type “guide” “Guide” creates interface on a Figure window and code in an M-file. Some hidden code.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Introduction to MATLAB Damon Tomlin February 22, 2008 Lecture 3: Data Visualization & User Interfaces.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Intro to Simulink April 15, Copyright , Andy Packard. This work is licensed under the Creative Commons.
Introduction to Matlab Module #10 Page 1 Introduction to Matlab Module #10 – Creating Graphical User Interfaces Topics 1.Overview of GUI Development using.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
OOP Basics Classes & Methods (c) IDMS/SQL News
Intro to Simulink Modified by Gary Balas 20 Feb 2011 Copyright , Andy Packard. This work is licensed under.
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
E177, Rules of Interruptibility of Callbacks March 4, Copyright , Andy Packard. This work is licensed.
More Matlab Graphics and GUI Graphics subplots some useful commands 3D graphics GUI GUI controls The callback property Other essential properties.
Graphics Intro UC Berkeley Fall 2007, E7 Copyright , Andy Packard and Roberto Horowitz. This work is licensed under the Creative Commons Attribution-ShareAlike.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Graphical User Interfaces I
Topics Graphical User Interfaces Using the tkinter Module
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
More on Graphical User Interfaces
Reusable Graphics Objects UC Berkeley Fall 2004, E77 me
Lesson 4 Using Basic Formulas
E177, Graphics Objects in Matlab February 26, me
E177, Reference/Assignment for Classes: Intro February 12, Copyright , Andy Packard. This work.
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

HandleGraphics Intro 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.

Graphical Objects in Matlab When we plot data in Matlab, we create a line object (and an axes object, and a figure object). The handle of the line object is the output argument for plot >> a = linspace(0,2*pi,500); >> b = sin(a); >> H = plot(a,b); The value of the variable H is the “handle of the line object” created by plot. The value is a unique identifier (an “address” or “pointer”) of the specific line that was drawn. >> set(H,’LineWidth’,4); >> set(H,’Color’,[1 0 0]); >> set(H,’Visible’,’off’); >> set(H,’Visible’,’on’)

Is there class? For some reason, no. The variable returned by plot is simply an object of class double. >> H = plot(a,b); >> class(H), size(H), H So, the variable H is not the line object. The value of H is the address of the line object. We say the “value of H points to the line object” or just “ H points to the object.” Clearing the variable does nothing to the line itself. >> whos >> clear H >> whos The variable is gone, but the line object remains (look at the figure).

Access the properties with the handle Use get to discover the public properties of the object. >> PubProp = get(H); >> get(H,’Type’) >> get(get(H,’Parent’),’Type’) Fieldnames are the properties, and values are current values. Change the properties with set >> set(H,’Linewidth’,3); Delete the object using delete. The variable H whose value is the handle of the object remains, but is now just a scalar double. >> whos % H is there, so is line >> delete(H) % line object disappears >> whos % H is still there >> get(H) % no graphics object % associated with this value

Creating in different workspaces Regardless of what is the “current” workspace when the object is created, the object is always accessible through its handle. >> a = 1:.01:10; >> b = cos(a); >> mkplot(a,b); After function exits, the line still exists, unlike the function workspace. Using get and the ’Children’ property, we can get its handle. >> F = gcf % GetCurrentFigure >> A = get(F,’Children’) >> H = get(A,’Children’) >> get(H,’Type’) function mkplot(x,y) plot(x,y)

Globally accessible hidden data “Within each” graphics object, you can store hidden, globally accessible data –Use the command setappdata. –This is called application data. Example >> A = rand(40,40); A(3,2) >> setappdata(H,’BigArray’,A); >> clear A >> whos % data is not in workspace Retrieve the data using getappdata. >> tmp = getappdata(H,’BigArray’); >> tmp(3,2) Data is “globally accessible” in that only the handle of the object is needed to access its application data.

Button-press events for line objects If the mouse is pressed while the pointer is over the line –a ButtonDownFcn event occurs, so… –if a ButtonDownFcn callback has been set, it is executed The callback may be set in two manners –using a char –using a cell In the case of a char, the string is evaluated in the base workspace >> cbstr = [’disp(’’Mouse pressed’’);’]; >> set(H,’ButtonDownFcn’,cbstr); Now, if the mouse is pressed over the line, the command eval(cbstr) will be executed “in the base workspace”, ie., as though you typed >> eval(cbstr) unless other callbacks are still executing. There is an event queue where unprocessed events are handled/scheduled in a defined fashion. More later...

Button-press events for line objects Change it to keep track of the number of presses >> cbstr = [’np = np+1;’]; >> set(H,’ButtonDownFcn’,cbstr); >> np = 0; Now, if the mouse is pressed over the line, the command eval(cbstr) will be executed in the base workspace, i.e., as though you typed >> eval(cbstr) % same as np = np + 1; Do it a few times, and it works. Easy to understand, but often not flexible enough…

Summary: All graphical objects in Matlab –are referred to by a handle (also called pointer or address) –have a get/set interface by which their properties are accessed and modified –are created by constructor methods, but are not variables not being variables, they are not in any workspace being created with constructors, they are created from a specific workspace, but exist independent of workspace. –can store hidden, globally accessible data, called application data (in the form of Matlab variables) within them that exists as long as the object itself exists. By “within them” we mean Variables are not in any workspace Variables exist as long as the object exists Variables are accessed/deleted/etc by knowing the object’s handle –Have events associated with them Example: a pushbutton object has a ButtonPress event; a Figure object has a MouseMotion event, etc. Programs, called “callbacks” can be associated with events so that when the event occurs, the callback program is executed

Graphical objects.vs. Variables Contrast the attributes of the graphical objects with the attributes of Matlab variables (like double, cell, char, struct and objects of user-defined classes) Graphics Objects: –Are referred to by an address, called the “handle” –Have a get/set interface to access/modify –Are not associated with a workspace –Can hide data –Can generate events Matlab variables: –Are referred to by name –Are referenced directly to access/modify ( double, cell, etc) –Live in a workspace –Cease to exist when the workspace they live in is deleted.

Graphical Objects in Matlab Some of the different types of graphical objects in Matlab are axes uicontextmenu uimenu figure uicontrol line patch surface text Matlab session, the Root object child of root children of figure children of axes Handle equals 0 … …

Root Object Constructor –There is none, it is created when Matlab is started Properties (try get(0) and set(0) to discover them) –Diary, DiaryFile –Format, FormatSpacing –PointerLocation, PointerWindow –RecursionLimit

figure Objects Constructor –figure –Automatically constructed when creating an axes, uicontrol… Properties (lots…) –Position –Color –CurrentPoint –HandleVisibility Events/Callbacks –KeyPressFcn –CloseRequestFcn –ResizeFcn –WindowButtonDownFcn –WindowButtonMotionFcn –WindowButtonUpFcn

axes Objects Constructor –axes Properties –CameraPosition –CurrentPoint –NextPlot –XTick, XTickLabel, XScale, XLim, XGrid, XDir –YTick, YTickLabel, … –ZTick, ZTickLabel, … –…–… Events/Callbacks –ButtonDownFcn –DeleteFcn

uicontrol Objects Constructor –uicontrol Properties –Style checkbox, pushbutton, edit, text, frame, popupmenu, listbox, radiobutton, slider, togglebutton –ToolTipString –Value –Enable Events/Callbacks –Callback –DeleteFcn –ButtonDownFcn

Position and Units properties Several objects (figure, axes, uicontrol) have properties named ’Position’ and ’Units’. How are these interrelated? If F is the handle of a figure, then get(F,’Position’) is the position (LLX,LLY,Width,Height) relative to the LL corner of screen, expressed in the units of get(F,’Units’) Note that get(0,’Units’) plays no role

Position and Units properties If UI is the handle of a uicontrol, then get(UI,’Position’) is the position (LLX,LLY,Width,Height) relative to the LL corner of parent figure, expressed in the units of get(UI,’Units’) Note that get(get(UI,’Parent’),’Units’) plays no role. If A is the handle of a axes, then get(A,’Position’) is the position (LLX,LLY,Width,Height) relative to the LL corner of parent figure, expressed in the units of get(A,’Units’) Again get(get(A,’Parent’),’Units’) plays no role.

CurrentPoint properties If F is a figure handle, then get(F,’CurrentPoint’) –is the pointer location (X,Y) relative to the LL corner of screen, expressed in the units of get(F,’Units’) If A is an axes, handle, then get(A,’CurrentPoint’) –is a 2x3 array of the “position” of the pointer, in the coordinates of the axes. –Take line perpendicular to screen, passing through pointer. Here get(A,’Units’) plays no role. Both are updated on buttonclick events, such as ButtonDownFcn, WindowButtonMotionFcn

Putting it all together Creating reusable interfaces requires some careful thought. Its best to integrate the graphical objects with the user defined classes. This would take a few more lectures. We’ll look at a very simple example next time. The overall philosophy will be apparent.

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.

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)

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…

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

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,,A2,A3,A4,A5 ) function ARG5(A1,A2,A3,A4,A5)... ARG5.m

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 5 th 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’) )

PushButton/Counter 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 frame text pushbutton uicontrol

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)

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

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 Retrieve ToolState Update ToolState Do calculations, set subobject properties Set callback events & subobject properties Run user callback if it exists, passing supplied arguments

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 ToolHan = e77gui1(IncCB) % Omitted Code PlusH = uicontrol('style','pushbutton',... % Omitted Code 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