200 likes | 404 Views
Scientific Programming Using MATLAB, Fall 2011-2012. TIRGUL #6: Handle graphics. Advanced graphics. So far we used built-in MATLAB functions to modify our graphics Access to MATLAB’s full graphical capacity is gained using an “object-oriented” approach .
E N D
Scientific Programming Using MATLAB, Fall 2011-2012 TIRGUL #6: Handle graphics
Advanced graphics • So far we used built-in MATLAB functions to modify our graphics • Access to MATLAB’s full graphical capacity is gained using an “object-oriented” approach
Object-oriented vs. procedural programming • Objects (very, very simplified): • Computational entities that have certain properties associated with them • MATLAB uses (or is) a procedural programming language. But figures and graphs can be treated as objects, with a fixed set of properties that can be manipulated.
Objects - examples Object of type ‘text’ Properties: FontName, FontSize, etc. Object of type ‘figure’ Properties: position, Color, etc. Object of type ‘line’ Properties: MarkerSize, LineStyle, etc. Object of type ‘axes’ Properties: XTickLabel, YTickLabel, Color, etc.
Objects – examples (continued) • Note: all the objects we discussed have the property ‘Color’ – but it accounts for something different in each of them.
Object handles • To interact with an object, we use handles • Handle = a unique identifier to an object that is set by MATLAB when the object is created • This ID is simply a scalar • Interaction = getting or setting property values, using the commands get/set • handle = figure(1); • h = plot(x,y) • H = title(‘string’) These variables hold the handle value of the graphics object you created
get • returnedValue = get(objectHandle, ‘propertyName’) • Example: • myAxisMarker = get(axisHandle, ‘Marker’) • myAxisMarker ‘pentagram’
Get all object properties • get(objectHandle) • Example: • get(lineHandle) etc.
set • set(objectHandle, ‘propertyName’, propertyValue) • Example: • set(textHandle, ‘FontSize’, 15) • propertyName is always a string • propertyValue depends on the property • set(textHandle, ‘Color’, ‘r’) • set(textHandle, ‘Color’, [1 0 0])
Finding out all the possible values for properties • set(objectHandle, ‘propertyName’) • Example: • set(lineHandle, ‘Marker’) >> [ + | o | * | . | x | square | diamond | v | ^ | > | < | pentagram | hexagram | {none} ] • set(lineHandle, ‘LineStyle’) >> [ {-} | -- | : | -. | none ] • All possible values for all properties: • set(objectHandle)
Handle values • Handle values should ALWAYS be kept as variables • handle = figure(1); handle = 1; • set(1, ‘Color’, ‘r’). No problem • h = plot(x,y); h = 301.0011 which is actually 301.00114820204….. • set(301.0011484…, ‘Color’, ‘r’). Problem!
Useful shortcut commands • gcf: get current figure – returns handle of current figure • gca: get current axes – returns handle of current axes • gco: get current object – returns handle of current object
Object hierarchy root figure axes UI-control UI-menu UI-contextmenu line light image patch surface rectangle text
Object hierarchy, continued • Each object inherits the default properties of its parent object • An object can have several children but only one parent • Note: h can be an array • get(axesHandle, ‘Children’) • If h is an array, property values will be set for all object handles in h root figure axes text image line
Example: • handleGraphics.m
RGB • Color can be specified in MATLAB using a short/long name, or an RGB triplet • RGB = Red, Green, Blue • The triplet is a vector of 3 values from 0 to 1, specifying the desired Color.
Color gradients • Use colormap functions (hot/ bone/ pink/ summer…) • Use ‘for’ to loop over RGB values, to create your own custom gradient • * Example: • * Pink(13)
Histograms – a clarification • a = randn(1000,1); • linspace(a, b, n) create a vector that has nlinearly spaced elements between a and b • logspace
Practice • Create a figure that has a 2*1 sub-plot array. • In the 1st sub-plot, create two line-plots, as you like • In the 2nd sub-plot, create an image-object using the command ‘imagesc’ • Give each sub-plot a title • Get all the handles of all the objects you created, and assign them to properly named variables • The objects are: 1 figure, 2 axes, 2 lines, 1 image, 2 texts • Change, using the handles and the command ‘set’: • figure background color to blue • Both line-plot styles to whatever you like • 1st title to italics, 2nd title to bold • Image property ‘CDataMapping’ from ‘scaled’ to ‘direct’ • What does this do?