Matlab tutorial course Lesson 5: Loading and writing data, producing visual output

Slides:



Advertisements
Similar presentations
Introduction to Formatting VistaPHw Charts Brought to you by: The Vista Partnership February 2007.
Advertisements

Microsoft® Office Outlook® 2003 Training
Introduction to MATLAB The language of Technical Computing.
Chapter 3 Creating a Business Letter with a Letterhead and Table
CS231A Matlab Tutorial Philip Lee Winter Overview  Goals › Introduction to Matlab › Matlab Snippets › Basic image manipulations › Helpful Matlab.
Tutorial 8: Developing an Excel Application
Copyright 2003 Peter McDevitt 1 Microsoft Excel 2002 Lecture 3 – A Professional Looking Worksheet.
® Microsoft Office 2010 Excel Tutorial 2: Formatting a Workbook.
DATA RETRIEVAL AND GUI CREATION DAVID COOPER SUMMER 2014.
MATLAB’s extensive, device-independent plotting capabilities are one of its most powerful features. They make it very easy to plot any data at any time.
Microsoft Excel 2010 Chapter 7
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Excel 2003 Lab 2 Charting Worksheet Data.
1 Introduction to MatLab MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix.
MATLAB Week 3 17 November Outline Graphics – Basic plotting – Editing plots from GUI – Editing plots from m-file – Advanced plotting commands.
SPSS Statistical Package for the Social Sciences is a statistical analysis and data management software package. SPSS can take data from almost any type.
XP New Perspectives on Microsoft Office Excel 2003 Tutorial 4 1 Microsoft Office Excel 2003 Tutorial 4 – Working With Charts and Graphics.
Access Tutorial 3 Maintaining and Querying a Database
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
FIRST COURSE Excel Lecture. XP 2 Introducing Excel Microsoft Office Excel 2007 (or Excel) is a computer program used to enter, analyze, and present quantitative.
Microsoft Office Word 2013 Expert Microsoft Office Word 2013 Expert Courseware # 3251 Lesson 4: Working with Forms.
Pasewark & Pasewark 1 Word Lesson 6 Working with Graphics Microsoft Office 2007: Introductory.
An introduction to Plotting in MATLAB Rikard Johansson Department of Biomedical Engineering (IMT) Linköping University
Microsoft Office 2007: Introductory 1 Word Lesson 6 Working with Graphics Computer Applications 1.
Instructor: Professor Cora Martinez, PhD Department of Civil and Environmental Engineering Florida International University.
Lesson 1 – Microsoft Excel The goal of this lesson is for students to successfully explore and describe the Excel window and to create a new worksheet.
Unit J: Creating a Database Microsoft Office Illustrated Fundamentals.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
XP New Perspectives on Microsoft Access 2002 Tutorial 51 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and.
PLOTS AND FIGURES DAVID COOPER SUMMER Plots One of the primary uses for MATLAB is to be able to create publication quality figures from you data.
Key Applications Module Lesson 16 — Excel Essentials Computer Literacy BASICS.
MICROSOFT WORD GETTING STARTED WITH WORD. CONTENTS 1.STARTING THE PROGRAMSTARTING THE PROGRAM 2.BASIC TEXT EDITINGBASIC TEXT EDITING 3.SAVING A DOCUMENTSAVING.
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.
Microsoft Excel Spreadsheet Review. Templates  Templates can be produced for the following elements:  Text and Graphics  Formatting Information – Layouts,
CHAPTER 9 Introducing Microsoft Office Learning Objectives Start Office programs and explore common elements Use the Ribbon Work with files Use.
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Microsoft ® Office OneNote ® 2003 Training Get to know OneNote CGI presents:
Microsoft ® Office Excel 2007 Working with Charts.
COMP 116: Introduction to Scientific Programming Lecture 5: Plotting, Scripts and publishing.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Key Applications Module Lesson 21 — Access Essentials
Lesson 11: Looking at Files and Folders what a file or folder is on the computer how to recognize a file or folder on the desktop how to recognize the.
Post-Processing Output with MATLAB Claudia Fricke Institute of Petroleum Engineering, Heriot Watt University.
Microsoft Access 2010 Chapter 8 Advanced Form Techniques.
Introduction MATLAB stands for MATrix LABoratory.  Basics  Matrix Manipulations  MATLAB Programming  Graphics  Image types  Image Processing  Useful.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
Return to Outline Copyright © 2011 by Maribeth H. Price 3-1 Labeling and annotation.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
MapInfo Professional 11.0: getting started Xiaogang (Marshall) Ma School of Science Rensselaer Polytechnic Institute Friday, January 25, 2013 GIS in the.
PowerPoint Lesson 6 Working with Tables and Charts Microsoft Office 2010 Advanced Cable / Morrison 1.
Written by: Itzik Ben Shabat Technion - Israel Institute of Technology Faculty of Mechanical Engineering Laboratory for CAD & Lifecycle Engineering Lab.
Excel 2007 Part (3) Dr. Susan Al Naqshbandi
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Files Tutor: You will need ….
Matlab tutorial course Exercises 5: Loading and writing images
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
An Introduction to Programming in Matlab Emily Blumenthal
Chapter 7 Creating Templates, Importing Data, and Working with SmartArt, Images, and Screen Shots Microsoft Excel 2013.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Reading and Writing Image Files
Lecture 25.
Word Lesson 6 Working with Graphics
CSE 307 Basics of Image Processing
Using Charts in a Presentation
Microsoft Office Illustrated Fundamentals
Unit J: Creating a Database
Presentation transcript:

Matlab tutorial course Lesson 5: Loading and writing data, producing visual output

Lesson overview Loading and writing data – Matlab data files: loading and saving – Images: loading, writing, converting types – Using dir to loop through files in a folder Producing visual output – Plotting data – Displaying images – Plotting on top of images – What’s really going on? – Saving plots

Matlab’s save command Save allows us to write any variable to disk, e.g. saving the results of an experiment save(‘filename’), – writes the entire contents of the workspace From the command line, we can also use the format: save filename – Note the lack of quotes around filename, and the space Note that these files are saved in a Matlab specific format that can’t (usually) be read by other programs The files will be save with a.mat extension – Note Windows will often try and associate these Microsoft Office Access – very annoying!

Matlab’s save command To save specific variables (typically more useful), include them in quotes, separated by commas after the filename save(‘filename’, ‘var1’, ‘var2’, ‘var3’) save filename var1 var 2 var3 We can also use the wildcard character * save(‘filename’, ‘var*’) save filename var* This means, save any variable whose name begins ‘var’

Matlab’s load command We can use the load command to read any saved.mat file load(‘filename’) load filename This loads all of the variables in the file into the current workspace We can also specify individual variables to load, in the same way as save load(‘filename’, ‘var1’, ‘var2’) – Note this means we need to know in advance what variables were saved. E.g. try A = rand(5); save(‘my_data.mat’, ‘A’); load(‘my_data.mat’, ‘fred’);

Matlab’s load command We can also specify an output from load – var = load(‘filename’, ‘var1’, ‘var2’); – This creates a structure (remember them?) called var, with fields, var1 and var2 Try A = rand(5); B = rand(6); C = ‘hello’; save(‘my_data.mat’, ‘A’, ‘B’, ‘C’); loaded_data = load(‘my_data.mat’) In some circumstances we can use load to read in data from other data types. Tutors on other courses may use this approach but we won’t cover it here Also, if you have a folder open, or use the current folder window in the main Matlab display, you can drag a.mat file into the main command window to load it

Reading and writing images Very simple! imwrite(im_var, ‘filename’, ‘fmt’); – Note you can usually leave out ‘fmt’ if this can be inferred from the extension on the filename (e.g..bmp,.jpg,.png etc) – We will cover what limits there are on the range and format of im_var next lesson im_var = imread(‘filename’); – Note it is usually NOT a good idea to name the variable you have assigned to as ‘image’, because there is a commonly used function in Matlab called ‘image’

Using dir A common task is to process a set of files (e.g. images) from a folder The function dir provides a list of all the files in a folder The output is stored in a structure with fields giving details of the file – In particular, the file name is stored in the field name It is best used with the wildcard * to match all files of a particular type – file_list = dir(‘folder_name/*.png’) – returns details of all the PNG images in ‘folder_name’

Using dir in a loop im_list = dir(‘folder/*.png’); for i_im = 1:length(im_list); %Load image im = imread([‘folder/’ im_list(i_im).name]); %Lines of code that do stuff with image … end Having used dir, we can then loop through each file to process it

Converting Microsoft Excel data We’re not going to cover this now… … but you can do it easily! Checkout the files xlsread and xlswrite for how

Try the first part of this week’s tutorial

Plotting data We’ve already looked at the ‘plot’ function Let’s explore in more detail – Plotting lines (‘-’, ‘--’, ‘-.’) – Plotting markers (‘x’, ‘+’, ‘^’, ‘v’, ‘s’, ‘o’, ‘*’) – Using hold to add multiple plots to a set of axes hold on, hold off, hold all – Adding titles, axes labels – Adding a legend See examples in ‘plotting_data.m’

Displaying Images An image can either be – A 2D array Use a ‘colormap’ to define how values are displayed as colors – An n x m x 3 array Each ‘slice’ is interpreted as red, green, blue channel If, double, values must lie in range [0, 1] If, uint8, values must lie in range [0, 255]

Three main image functions 1.image(A); – Use default colormap ‘jet’ with 256 colours – Assumes intensity scale from 0 to 255 – Resizes figure in each direction to fit available space 2.imagesc(A); – Scales image values to fill colormap I.e. uses intensity scale from min(A(:)) to max(A(:)) 3.imshow(A); – Maintains aspect ratio of image – Uses default colormap ‘gray’ with 256 colours – Scales image based on data type If double, assumes scale [0, 1] If uint8, assumes scale [0, 255] See examples in ‘displaying_images.m’

Plotting on top of images Of course, we can mix the plotting and image functions – Annotate images – Check points of interest we’ve detected match up to the image See plotting_on_images.m

Plotting on top of images (cont) Note that for images, by default, the y-axis increase from top to bottom When plotting without images, the y-axis increases from bottom to top Be careful – especially when applying transformations to points – E.g. rotations Also, images, as 2D arrays, are indexed matrix style (row, column) Functions to do with plotting data, manipulating coordinates etc, are usually indexed (x, y) – Equivalent to (x=column, y=row) in an image

What’s really going on? 3 levels of objects… Figure object Axes Axes object Axes Line series object Image object Axes Text object hello

What’s really going on? All the plotting and imaging functions we have used so are ways of creating and manipulating these objects Most the time we can let Matlab use default settings, and use the inbuilt functions to modify particular properties It is helpful to know what properties belong to which object We can then manipulate directly if we want – Better understanding of what’s going on – More flexibility than just using shortcut functions

Figure properties A figure is the main window in which your visual output is displayed We create a new object using ‘figure’ Key properties include – Colormap (this means we can only have 1 per window) – Size and position in screen – Real world size when saved as an image Use Matlab help to see full list

Axes properties Each figure can contain one or more axes Axes define a region in which plots, images and text are drawn Create a new object using axes – This adds the axes to the current figure object – If no figure object exists, a new one is created Key properties – Limits in x, y (and z) directions – Scaling of each axis – Direction of each axis (especially – Size and position within figure window – Whether new line, text or image objects are added or replace what’s there

Axes properties (cont.) Key properties – Axes labels – Axes title – Tick points on axes On/off or positions Labels of tick points – Scaling of values to figure colormap Allows us to cheat at having two colormaps in a single figure – Font sizes of labels, titles etc See Matlab help for full list of properties

Line/image/text objects The actual content we see Created using plot, image, imagesc, imshow etc – The ‘primitive’ functions are ‘line’, ‘image’, ‘text’ The other functions call these and then set some properties (including those in the parent axes/figure) – When created are added to the current axes This will replace the current contents if ‘hold’ property of axes not set – If no axes, Matlab will create a new axes This in turn may create a new figure Line objects, generally created using ‘plot’ – (although we can call ‘line’ directly) Image objects created using ‘image’

Line series properties Coordinates of each line segment Line color Line style (or none) Marker type (or none) Marker colour, size See Matlab help for full list of properties We haven’t covered surfaces (3D plots) – Can create some fun images – Search for ‘surf’ or ‘mesh’ to see some examples

How do we manipulate object properties? We need to introduce a new datatype… … a handle Each object has a unique identifier called a handle – Look again at the help documentation for ‘plot’ Note how it returns a handle as output – this is the identifier to the line series object created Can take a handle as input – this is the identifier of an axes to which it will be added – Likewise ‘f = figure;’ ‘a = axes;’ return handles to the objects we’ve just created – ‘gca’ and ‘gcf’ return the handles to the current axes and figure respectively We can use an object’s handle to directly manipulate its properties

Shortcut functions A lot of the visual output functions we’ve used so far provide shortcuts to setting object properties – xlabel(‘Time in s’); … set(gca, ‘xlabel’, ‘Time in s’); – colormap(jet(256));… set(gcf, ‘colormap’, jet(256)); – axis([x1 x2 y1 y2]);… set(gca, ‘xlim’, [x1 x2], ‘ylim’, [y1 y2]); We can set multiple properties at once, in any order – Remember the name/value pairs input from week 1? We can also use ‘get’ to retrieve the current value of any property See handles.m for more examples

Handles summary Handles are unique identifier to object – Image, line, text objects belong to… … axes objects, which belong to… …figure objects Use ‘set’, ‘get’ to manipulate properties Use help menu to explore properties of each object type

Saving plots Can use ‘Save as’ from file menu Or function ‘saveas(f1, ‘filename’); Often helpful to set some of the figure properties first – PaperSize, PaperPosition etc. – Non-compressing formats tend to look nicer Avoid jpg’s – Vector formats (pdf, eps, etc. allow infinite zooming when included in reports) Also helps to configure line width and marker size and font size Can also use copy figure from menu and paste directly in Word/PowerPoint etc.

Saving images Use imwrite – don’t need to generate figure first Remember the rules on what image types will display without a colormap? – Same applies for saving – Either save any 2D array and also pass in a colormap – Or ensure type and scale match uint8: [0, 255], double: [0, 1] etc. Directly manipulating RGB data allows you to be create with your colours If you have plots on top of your images, best to treat like a plot – Generate figure – Call saveas on the figure handle – See the suggestions on the previous line for properties, file formats etc.