One of the places where you can see object oriented concepts in Matlab is in the graphics created by Matlab. These features are extensive and Matlab offers unlimited access to it through Handle Graphics . Using the functions available here the graphical image can be modified in any conceivable way. Also knowledge of the functions in Handle Graphics will allow us to design GUI for the program or create graphics that is not standard in Matlab. Note these features are primitive compared to VB but they are reasonable and sufficient from a technical computation point of view. If you are using the full version of Matlab then you can use the GUI tool to build GUI's. If you are using the Student version then you are out of luck. These notes do not use the tool
Handle Graphics refers to collection of low level graphics functions that are used to generate the graphics. From the help desk the tree for the Handle Graphics (HG) objects are show belowFigure. Handle Graphics Object and Hierarchy HG is built on the consideration that each graphical element is an Object. As before every Object has a set of properties/data and method/procedures that operate on the data. Every graphics command creates an object.
The hierarchy above illustrates the Parent/Child relationship among the various graphical objects defined by Matlab.
HG works by identifying every object created through a unique Handle. The handle of the Root object is always 0 . To change properties or invoke methods for a specific object - for example change the color of the axes object - you will need to refer to the specific axes by its handle. This terminology is standard in all platforms.The Root object is the Computer screen.
All graphics objects will be contained in a Figure window
The Root object can have one or several Figure windows
Each Figure window can contain Axes, Uicontrol, Uimenu, Uicontextmenu
Each Axes can contain other objects including line, surface, text etc.
Before we proceed further lets examine some of the properties of these objects through the helpdesk ( browser version of help).
In the menu - Help Desk - Click on Graphic objects - You should see above figureWorking ith HG typically involves changing object properties you saw in the list previously. There are two functions available to accomplish this. They are get and set. Once again these functions are universal across most platfoms. VB actually has three - Get, Set and Let. We will accept that get will obtain the value of an object's property while set will cause the property to change to a new value.Click on the Figure Object
You should see a definition/description of the useful properties of the Figure object
The format for the get statement is : get ( handle, 'PropertyName' )
The format for the set statement is: set ( handle, 'PropertyName', Value [ handle, 'PropertyName', Value].... )
Lets see if we can understand this stuff by applying it to the Bezier curve we generated previously.
We will create a new script file
Run Matlab
Open new M-file - this should open the Matlab editor>> addpath C:\Emem899\Matlab_Course
Type the following script
%---------------------------------------------------------------------------
% script file for exploring Matlab Handle Graphics
%[file,path] = uigetfile('*.dat', ' Ascii data file',100,100);
%get the file XY1,datloadpathfile = ['load ',path,file]; % the space after d is important
eval(loadpathfile); % new command - evalutes the string and executes
% functions if present in stringwho % check the existence of XY1
get(H_fig,'Color') % get the Back color property of the figure
set(H_fig,'Color',[0.3 0.5 0.9]) % note that value of the color
% is a vector of 3 values for RGB
set(H_fig,'NumberTitle','off','Name','Testing Handle Graphics')
get(H_fig) % this should list all the current values for the properties
![]()
The following is the output - do not include it in your script fileBackingStore = on
CloseRequestFcn = closereq
Color = [0.3 0.5 0.9]
Colormap = [ (64 by 3) double array]
CurrentAxes = [7.00012]
CurrentCharacter =
CurrentObject = [8.00012]
CurrentPoint = [473 207]
Dithermap = [ (64 by 3) double array]
DithermapMode = manual
FixedColors = [ (10 by 3) double array]
IntegerHandle = on
InvertHardcopy = on
KeyPressFcn =
MenuBar = figure
MinColormap = [64]
Name = Testing Handle Graphics
NextPlot = add
NumberTitle = off
PaperUnits = inches
PaperOrientation = portrait
PaperPosition = [0.25 2.5 8 6]
PaperPositionMode = manual
PaperSize = [8.5 11]
PaperType = usletter
Pointer = arrow
PointerShapeCData = [ (16 by 16) double array]
PointerShapeHotSpot = [1 1]
Position = [436 283 560 420]
Renderer = painters
RendererMode = auto
Resize = on
ResizeFcn =
SelectionType = normal
ShareColors = on
Units = pixels
WindowButtonDownFcn =
WindowButtonMotionFcn =
WindowButtonUpFcn =
WindowStyle = normalButtonDownFcn =
Children = [ (2 by 1) double array]
Clipping = on
CreateFcn =
DeleteFcn =
BusyAction = queue
HandleVisibility = on
HitTest = on
Interruptible = on
Parent = [0]
Selected = off
SelectionHighlight = on
Tag =
Type = figure
UIContextMenu = []
UserData = []
Visible = on
![]()
H_plot = plot(XY1(:,1),XY1(:,2),'b-')
% matlab sets up the default values for all the graphic objects
% used to display the plot on the figure
% the plot object is contained in the axes objectH_ax = gca % get handle to current axes
set(H_plot,'Color',[1 0.5 0],'LineWidth',3)
H_xlab = get(gca,'Xlabel') % gets the handle for the child - XLabel
set(H_xlab,'FontSize',18,'String','X-Values')
set(get(gca,'YLabel'),'FontSize',14,'String','Y-Values','FontName','Arial')
set(gca, 'YAxisLocation','Right','YColor','r','YDir','Reverse','Box','Off')
set(get(0,'CurrentFigure'),'Position',[0 0 300 300])
% the Position property is a vector og [ left bottom width height]
![]()
% Default values
% matlab has built in default values for all the properties.
% You can set your own default properties using a special
% property-name string concatenated with 'Default' plus
% 'Object type' plus 'PropertyName'% For example - DefaultFigureColor
% you can place these in the startup file for effectiveness
set(0,'DefaultAxesXGrid','on')
set(0,'DefaultAxesYGrid','on')
set(0,'DefaultAxesFontSize',14)
set(0,'DefaultAxesXColor','y')
set(0,'DefaultAxesYColor','g')
set(0,'DefaultFigureColor',[0.5 0.5 0.5])
Matlab provides reasonable facility for greating GUI. As an engineer there probably may not be a reason to develop a GUI - after all we can read information into Matlab through Stdin(typing at screen prompts) or through reading files. In the event you have a program that several people can use, or you have a utility that you will use many times, or you want to create an interactive program, or you want to impress your boss..Knowledge og HG is very important in designing and implmenting a GUI in Matlab.
Once again using the help desk the following information can be located regarding GUI creation
![]()
![]()
Much of the Controls needed for the GUI are in the uicontrol object
![]()
While it will be difficult to explore all the items here we will look at some useful ones.
The important thing to remember about GUI objects that we place on the screen is that:
The most difficult thing about GUI creation is the 'Callback' string. Some of the things to remember about the implementation of Callback are:All objects should be associated with a handle
Most objects must have a name to display
All objects must be associated with a Callback string/function. This is the string that eval will execute in response to the user event.
The GUI is best designed using a Script file rather than interactively
The Callback is a string that will be passed to the eval function when the event corresponding to the control is triggered
For multiple MATLAB Commands/Statements enclose the entire callback string in square brackets. Make sure to include the final )
Enclose each MATLAB statement in 'single quote'
Quotes within a quoted string have double quotes like ' ' String ' '
Each statement will have a Comma or Semicolon within the quote - and a comma or space after the qoutes
Each line to be continued ends with three periods . . .
The Callback can be executed using (i) Scripts or (ii) Using Callback functions. We will use the first option
A very important consideration is that Callback strings are passed to eval and executed in the Command Workspace, while the rest of the code executes in the function workspace- including the GUI script function
Variables defined within functions are not availble in the Command Workspace and variables used in the Command workspace are not available in the function workspace.
Global variables can be used to transfer this data
Since GUI control elements report strings - particular attention must be paid to string to number conversion for using the numerical data
There is a lot more to know about GUI and Callback. Here we will only deal with basic design. In the next exercise we will construct a working GUI for the TPBVP program we developed in Matlab.
![]()