1 / 20

Scientific Programming Using MATLAB, Fall 2011-2012

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 .

langston
Download Presentation

Scientific Programming Using MATLAB, Fall 2011-2012

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. Scientific Programming Using MATLAB, Fall 2011-2012 TIRGUL #6: Handle graphics

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

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

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

  5. Objects – examples (continued) • Note: all the objects we discussed have the property ‘Color’ – but it accounts for something different in each of them.

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

  7. get • returnedValue = get(objectHandle, ‘propertyName’) • Example: • myAxisMarker = get(axisHandle, ‘Marker’) • myAxisMarker  ‘pentagram’

  8. Get all object properties • get(objectHandle) • Example: • get(lineHandle) etc.

  9. 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])

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

  11. 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!

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

  13. Object hierarchy root figure axes UI-control UI-menu UI-contextmenu line light image patch surface rectangle text

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

  15. Example: • handleGraphics.m

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

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

  18. Histograms – a clarification • a = randn(1000,1); • linspace(a, b, n)  create a vector that has nlinearly spaced elements between a and b • logspace

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

More Related