Presentation is loading. Please wait.

Presentation is loading. Please wait.

Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab.

Similar presentations


Presentation on theme: "Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab."— Presentation transcript:

1 Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab 2 : Graphic Window & 2D Primitives

2 Written by: Itzik Ben Shabat Tutorial Topics Submission notes Matlab Graphics basics Data preparation Window properties Axes Properties Drawing - Geometric 2D Primitives Axis properties Graphic handles Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

3 Written by: Itzik Ben Shabat Submission notes Create a new scrip file and name it “yourname_YourID_LAB2” You will submit this file at the end of the lesson so please follow instructions carefully Insert the following code at the beginning of the script %clear variables and command line and close all windows clear all; close all; clc; %%%%%%initializing variables%%%%%% Each time you see code in red you should implement it in your script. Make sure to write notes to go along with it Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

4 Written by: Itzik Ben Shabat Matlab Graphics Basics MATLAB allows the visualization of data In this course our data is geometric (location in 3D space/on the 2D plain) e.g (x,y,z) coordinates Example: An engineering student conducted an experiment of measuring engine temperature over time. Data : time=[0 1 2 3 4 5 6 7 8 9]; temp=[0 100 200 300 400 500 600 700 800 900]; Question: How would you visualize this data? Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

5 Written by: Itzik Ben Shabat Matlab Graphics Basics Answer: line graph The data is represented by 2D points (time is x axis and temp is y axis) Question: How do we plot a graph in MATLAB ? Partial Answer: Plot(time,temp); Question : why partial ? Answer : This line of code will plot the graph. However the result is missing a lot of information Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

6 Written by: Itzik Ben Shabat Matlab Graphics Basics This is the output result Question: What information is missing? Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

7 Written by: Itzik Ben Shabat Matlab Graphics Basics Student Answers: Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

8 Written by: Itzik Ben Shabat Matlab Graphics Basics Answer: Figure (window) – title, size, initial position. Background color, plot position within the window Axis Annotations and properties – graph title, axis labeling, legend, text, limits, tick marks, grid lines Line characteristics – width, size (markers), color Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

9 Written by: Itzik Ben Shabat Matlab Graphics Basics 5 main stages visualization: 1. Prepare data 2. Set Figure (window) properties 3. Set axes properties 4. Drawing 5. Set axis properties In the following slides we will learn how to do all of the above Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

10 Written by: Itzik Ben Shabat Matlab Graphics Basics 1. Preparing data Depending on the plotting function you will use you might need to rearrange, interpolate or extrapolate your data In our example, we can combine the data to be a nX2 matrix – each row contains the x and y coordinates of each point (n represents the number of points) XY=[time’,temp’]; The transpose operator ‘ – converts row vector to column vector Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

11 Written by: Itzik Ben Shabat Matlab Graphics Basics 2. Set Figure Properties figure Creates figure graphics objects. Figure objects are the individual windows on the screen in which graphical output is displayed Figures can contain graphs and GUI components (more on GUI in future tutorial) This function returns a handle for the figure that was created. Figure handle is the identifier of the figure (useful when working with more than one figure. The function returns a figure handle gcf Returns the active figure handle Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

12 Written by: Itzik Ben Shabat Matlab Graphics Basics 2. Set Figure Properties figure('PropertyName',propertyvalue,...) sets the figure properties For a full property list visit linklink Commonly used properties are summarized in next slides table. The property default value is the first from the left Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

13 Written by: Itzik Ben Shabat Matlab Graphics Basics 2. Set Figure Properties Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering What does it do?Property values Property Name Determines whether the string Figure No. N (where N is the figure number) is prefixed to the figure window title ‘on’ |’off’ NumberTitle Specifies the title displayed in the figure. Window. (Empty by default) string Name More on this subject in future tutorials painters | zbuffer | OpenGL Renderer Determines if you can resize the figure window with the mouse ‘on’ |’off’ Resize

14 Written by: Itzik Ben Shabat Matlab Graphics Basics 2. Set Figure Properties Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering What does it do?Property values Property Name Specifies the size and location on the screen of the full figure window (left and bottom are with respects to the screends bottom left corner). Includes all window elements (title bar, toolbars and outer edges). Units are defined by Units property [left, bottom, width, height] OuterPosition Same as OuterPosition only does not Includes all window elements (title bar, toolbars and outer edges) [left, bottom, width, height] Position Controls the figure window background colorColorSpec Color

15 Written by: Itzik Ben Shabat Matlab Graphics Basics 2. Set Figure Properties ColorSpecColorSpec – Three ways to specify color for MATLAB grphics Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Long NameShort NameRGB Value yellowy[1 1 0] magentam[1 0 1] cyanc[0 1 1] redr[1 0 0] greeng[0 1 0] blueb[0 0 1] whitew[1 1 1] blackk[0 0 0]

16 Written by: Itzik Ben Shabat Matlab Graphics Basics 2. Set Figure Properties Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering What does it do?Property values Property Name Specifies the units MATLAB uses to interpret size and location data pixels | inches | centimeters | normalized | points | characters Units

17 Written by: Itzik Ben Shabat Matlab Graphics Basics 2. Set Figure Properties In our example lets set the following properties Window location at 100pxls from the bottom and 100pxls from left 500X500 size (including all elements) title ‘yourname set fig props’ Hide the figure number Allow resizing Set background color to cyan (use ShortName representation) Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

18 Written by: Itzik Ben Shabat Matlab Graphics Basics 2. Set Figure Properties Answer code %2. set figure properties only figure('NumberTitle','off','Name','Itzik set fig props'...,'OuterPosition',[100 100 500 500],'color','c'); Note: For neater code you can use … and then continue the code in the next line (prevents very long lines that require scrolling) Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

19 Written by: Itzik Ben Shabat Matlab Graphics Basics 2. Set Figure Properties Run and compare outputs Should look like this Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

20 Written by: Itzik Ben Shabat Matlab Graphics Basics 3. Set Axes Properties axes Creates an axes graphics object in the current figure using default property values Axes objects define the coordinate system for displaying graphs Axes are always contained within a figure The function returns an axes handle gca Retuns the handle of the current axes Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

21 Written by: Itzik Ben Shabat Matlab Graphics Basics 3. Set Axes Properties axes('PropertyName',propertyvalue,...) sets the axes properties For a full property list visit linklink Commonly used properties are summarized in next slides table. The property default value is the first from the left Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

22 Written by: Itzik Ben Shabat Matlab Graphics Basics 3. Set Axes Properties Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering What does it do?Property values Property Name Position of axes including labels, title, and a margin [left, bottom, width, height] OuterPosition Position of axes[left, bottom, width, height] Position

23 Written by: Itzik Ben Shabat Matlab Graphics Basics 3. Set Axes Properties More on aspect ration in future tutorials Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering What does it do?Property values Property Name Defines the axes bg colorColorSpec Color Defines the units used to interpret the Position property normalized | inches | centimeters | points | pixels | characters Units Relative scaling of data units[dx dy dz] DataAspectRatio Relative scaling of axes plot box[dx dy dz] PlotBoxAspectRatio

24 Written by: Itzik Ben Shabat Matlab Graphics Basics 3. Set Axes Properties %3. set figure and axes properties figure('NumberTitle','off','Name','Itzik set axes props'...,'OuterPosition',[600 100 500 500],'color','c'); In our example insert the above code and then set the following properties Define 2 coordinate systems and save their handle to ax1 and ax2 variables Set ax1 properties Green bg color (use long name representation) position (not outer position) axes lower left corner at window lower left corner Size of half the window size in X direction and half in y direction Set ax2 properties Blue bg color (use RGB representation) position (not outer position) axes lower left corner at window center Size of half the window size in X direction and half in y direction Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

25 Written by: Itzik Ben Shabat Matlab Graphics Basics 3. Set Axes Properties Answer code %3. set figure and axes properties figure('NumberTitle','off','Name','Itzik set axes props'...,'OuterPosition',[600 100 500 500],'color','c'); ax1=axes('Color','green','Position',[0 0 0.5 0.5]); ax2=axes('Color',[0 0 1],'Position',[0.5 0.5 0.5 0.5]); Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

26 Written by: Itzik Ben Shabat Matlab Graphics Basics 3. Set Axes Properties Run and compare outputs Should look like this Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

27 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing Generate a new figure and set Properties are the same except Name ‘your name plotting’ Position to be to the right of the last figure we plotted Generate a new axis and set Bg color to blue (any representation you like) Your output should look like this (in addition to previous figures Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

28 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing Answer Code %4. plot rectangle using line and rectangle w=500; h=500; xo=100; yo=100; figure('NumberTitle','off','Name','Itzik plotting'...,'OuterPosition',[xo+2*w yo w h],'color','c'); axes('Color','b'); Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

29 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing Many functions draw elements to axes (plot was already introduced) Try to explore scatter function The core drawing elements can be found in this linklink In this tutorial we will elaborate on the following 2D core objects Line Rectangle Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

30 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing line(X,Y ) adds the line defined in vectors X and Y to the current axes line(X,Y,Z,'PropertyName',propertyvalue,...) Creates a line using the values for the property name/property value pairs specified and default values for all other properties For a full property list visit the following linklink Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

31 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing - line Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering What does it do?Property values Property Name Specify this value in pointsscalar LineWidth Size of the marker in pointsscalar MarkerSize Specifies marks displayed at data points+ | o | * |. | x | s | d | ^ | v | > | < | p | h | none Marker The color of the marker or the edge color for filled markers ColorSpec MarkerEdgeColor The fill color for markersColorSpec MarkerFaceColor Specifies the line colorColorSpec Color

32 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing rectangle('Position',[x,y,w,h]) draws the rectangle from the point x,y and having a width of w and a height of h rectangle,'PropertyName',propertyvalue,...) draws the rectangle using the values for the property name/property value pairs specified and default values for all other properties For a full property list visit the following linklink Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

33 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing - rectangle Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering What does it do?Property values Property Name Specifies the curvature of the rectangle sides, which enables the shape of the rectangle to vary from rectangular to ellipsoidal one- or two- element vector [x,y] Curvature Specifies the color of the rectangle edgesColorSpec EdgeColor Specifies the color of the rectangle face, which is not colored by default. ColorSpec FaceColor Specify this value in pointsscalar LineWidth Specify rectangle location from bottom left x,y and having a width of w and a height of h [x,y,w,h] Position

34 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing Add code to draw a rectangle and set Fill color to be red Position the bottom left corner to be at 1,1 Width and height of 5 Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

35 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing Answer code %4. plot rectangle using line and rectangle w=500; h=500; xo=100; yo=100; figure('NumberTitle','off','Name','Itzik plotting'...,'OuterPosition',[xo+2*w yo w h],'color','c'); ax3=axes('Color','b'); rect=[1 1 5 5]; rectangle('Position',rect,'FaceColor','r'); Run this….. Whats wrong ? Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

36 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Drawing The axis min and max values change to fit just the rectangle. To fix this we need to set the axis properties Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

37 Written by: Itzik Ben Shabat Matlab Graphics Basics 5. Set Axis Properties There are a few ways to apply these properties we will demonstrate it using set(H, 'PropertyName',propertyvalue,…) sets the named properties to the specified values on the object(s) identified by H. AND get(h,'PropertyName') Returns the value of the property 'PropertyName' of the graphics object identified by h Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

38 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Axis Properties Note – These properties can be set in Axes as well Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering What does it do?Property values Property Name Sets axis maximal and minimal value[min max] Xlim | Ylim | ZLim The handle of the text object used to label the x-, y-, or z-axistext handle of text object Xlabel | Ylabel| ZLabel The handle of the text object used for the axes titletext handle of title object Title

39 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Axis Properties Set the following axes properties using set and get Graph title – ‘Red Rect’ Xlabel – ‘X’ Ylabel – ‘Y’ X &Y limits [0 10] Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

40 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Axis Properties Answer Code %5. set axis properties set(ax3,'XLim',[0 10],'YLim',[0 10],'Title',text('String','Red Rect'),... 'XLabel',text('String','X'),'YLabel',text('String','Y')); Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

41 Written by: Itzik Ben Shabat Matlab Graphics Basics 4. Axis Properties Your output should look like this at this point Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

42 Written by: Itzik Ben Shabat Matlab Graphics Basics 6. Graphic Handles MATLAB assigns a handle to every graphics object it creates. All object creation functions optionally return the handle of the created object If you wish to access an object props at any point, assign its handle to a variable when creating it Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

43 Written by: Itzik Ben Shabat Matlab Graphics Basics 6. Graphic Handles Question: Which handles did we save and did not save in this tutorial ? Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

44 Written by: Itzik Ben Shabat Matlab Graphics Basics 6. Graphic Handles Answer: We saved axes handles (ax1,ax2,ax3) We did not save the handle of our figures and objects (rectangle) It is good practice to always save the handles. Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

45 Written by: Itzik Ben Shabat Matlab Graphics Basics Practice Try to change the colors of the objects you created Try to change the dimensions and limits of objects and axes Read and explore more figure and axes properties Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

46 Written by: Itzik Ben Shabat Final code Your final code from this tutorial should look like this Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering

47 Written by: Itzik Ben Shabat Matlab Graphics basics Mathworks Documentation Center – PDF DocumentationPDF Documentation Rgb picture from http://en.wikipedia.org/wiki/RGB_color_modelhttp://en.wikipedia.org/wiki/RGB_color_model Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering


Download ppt "Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab."

Similar presentations


Ads by Google