Matlab Training Session 5: Importing Data

Slides:



Advertisements
Similar presentations
Exploring Microsoft Excel 2002 Chapter 7 Chapter 7 List and Data Management: Converting Data to Information By Robert T. Grauer Maryann Barber Exploring.
Advertisements

EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
Chapter 7 Data Management. Agenda Database concept Import data Input and edit data Sort data Function Filter data Create range name Calculate subtotal.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Fall 2006AE6382 Design Computing1 Matlab File & Directory Management Learning Objectives Define file input and output terminology Compare high and low.
Guide To UNIX Using Linux Third Edition
The textread Function It is designed to read ASCII files that are formatted into columns of data Each column can be of a different type It is useful for.
Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH)
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Managing Business Data Lecture 8. Summary of Previous Lecture File Systems  Purpose and Limitations Database systems  Definition, advantages over file.
THE MATLAB ENVIRONMENT VARIABLES BASIC COMMANDS HELP HP 100 – MATLAB Wednesday, 8/27/2014
Key Applications Module Lesson 16 — Excel Essentials Computer Literacy BASICS.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
Spreadsheet A spreadsheet is the computer equivalent of a paper ledger sheet. It consists of a grid made from columns and rows. It is an environment that.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
1 Performing Spreadsheet What-If Analysis Applications of Spreadsheets.
UNIT 7: Using Excel in the Law Office. This Week’s Assignment You should be working on your three-part assignment Part 1 deals with the things you learned.
Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers.
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 ….
CPSC 203 Introduction to Computers T97 By Jie (Jeff) Gao.
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
Using and Programming with MATLAB as an Engineering Tool [ Part III ]
Input Output Garbage In, Garbage Out. Outline Announcements: –HWII solutions on web soon –Homework III: due Wednesday Advanced ASCII Binary Basics Cell-arrays.
Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Wednesday Advanced ASCII Binary Basics Filesystem Fun.
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 Data Files
Multi-Axis Tabular Loads in ANSYS Workbench
Multiple Worksheets (with links)
EEE 161 Applied Electromagnetics
Prof. Mark Glauser Created by: David Marr
Matlab Training Session 4: Control, Flow and Functions
JSL File manager Brady Brady and Don Mccormack, JMP.
Microsoft Excel Basic Skills
Chapter 2: Getting Data into SAS
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
Matlab Workshop 9/22/2018.
REDCap Data Migration from CSV file
Jeff Henrikson Lecture 11 Data Import & Export Jeff Henrikson 1.
StatLab Matlab Workshop
Advanced Data Import & Export Jeff Henrikson
Fill-in-the-blank Computing: The Basics of Spreadsheets
Building Web Applications
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
File Input and Output.
Fill-in-the-blank Computing: The Basics of Spreadsheets
Matlab.
Introduction to MATLAB
Input/Output Functions
Fill-in-the-blank Computing: The Basics of Spreadsheets
Matlab Training Session 2: Matrix Operations and Relational Operators
Intro to Excel CSCI-150.
Key Applications Module Lesson 16 — Excel Essentials
Input/Output Functions
Unit G: Using Complex Formulas, Functions, and Tables
Spreadsheets and Data Management
Presentation transcript:

Matlab Training Session 5: Importing Data

Course Outline Weeks: Introduction to Matlab and its Interface (Jan 13 2009) Fundamentals (Operators) Fundamentals (Flow) Importing Data Functions and M-Files Plotting (2D and 3D) Statistical Tools in Matlab Analysis and Data Structures Course Website: http://www.queensu.ca/neurosci/matlab.php

Week 5 Lecture Outline Importing Data A. Week 4 Review B. Simple Import C. Handling Files D. Mini-Project

Functions in Matlab In Matlab, each function is a .m file It is good protocol to name your .m file the same as your function name, i.e. funcname.m function outargs=funcname(inargs); Function input output

Mini-Project Raising any number of numbers to the nth power Inputs: A vector of numbers to be raised (N1…Nm) A vector of powers (P1…Pm) Outputs: A vector of raised values (N1P1 … NmPm) An error flag: 1 if error in calculation, 0 if successful Caveats: If only one input is provided, the function should square each entry, so output = (N12…Nm2) and error flag is 0 If the length of N and P are not the same, this is an error, return anything in the output vector and a 1 in the error flag Make sure to comment and document the function

Solution Complex function [y, e] = raise(x,n) y = ones(1,length(x)); if nargin == 1 [y e] = raise(x,2*ones(1,length(x))); return elseif nargin == 2 if(length(x)~=length(n)) y = NaN; e = 1; end for(i=1:length(x)) for(j=1:n(i)) y(i) = y(i)*x(i); e = 0; Simple function [y, e] = raise(x,n) if nargin == 1 [y e] = x.^2; return elseif nargin == 2 if(length(x)~=length(n)) y = NaN; e = 1; end y = x.^n e = 0;

Importing Data Basic issue: Other Issues: How do we get data from other sources into Matlab so that we can play with it? Other Issues: Where do we get the data? What types of data can we import Easily or Not

load Command opens and imports data from a standard ASCII file into a matlab variable Restrictions Data must be constantly sized Data must be ASCII No other characters

load Consider the simple file below Create in notepad and save as test1.txt and text2.txt In matlab, set the path to the correct place (ie. where the file is) and type load(‘test1.txt’) Now, type x = load(‘test1.txt’) 1 2 3 4 5 2 4 8 16 32 test1

load Now the file is no longer simple because not every row has the same amount of characters Create in notepad and save as test2.txt and text2.txt type y = load(‘test2.txt’) Error! 1 2 3 4 5 4 8 16 32 test2

load Now type in the same thing from test1.txt into Excel and save the workbook as test1.xls type y = load(‘test1.xls’) What happens? Forcing the issue with Excel data test1 1 2 3 4 5 2 4 8 16 32

load Works for simple and unstructured code Powerful and easy to use but limited Will likely force you to manually handle simplifying data which is prone to error More complex functions are more flexible

File Handling f* functions are associated with file opening, reading, manipulating, writing, … Basic Functions of Interest for opening and reading generic files in matlab fopen fclose fseek/ftell/frewind fscanf fgetl

fopen Opens a file object in matlab that points to the file of interest fid = fopen(‘filepath’) absolute directory + filename If file of interest is C:\Andrew\Project_1\file.dat fid = fopen(‘C:\Andrew\Project_1\file.dat’) relative path + filename If your matlab path is set to c:\Andrew\Project_1 fid = fopen(‘file.dat’) fid is an integer that represents the file Can open multiple files and matlab will assign unique fids

fclose When you are done with a file, it is a good idea to close it especially if you are opening many files fclose(fid)

What is a File? A specific organization of data In matlab it is identified with a fid Location is specified with a pointer that can be moved around fid file_name Pointer

Moving the Pointer We already know how to assign a fid (fopen) To find where the file is pointing: x = ftell(fid) To point somewhere else fseek(fid,offset,origin) Move pointer in file fid by offset relative to origin Move pointer by a fixed number of bytes Origin can be beginning, current, end of file To point to the beginning frewind(fid)

Getting Data Why move the pointer around? Get somewhere in the file from where you want data [data] = fscanf(fid,format,size) Format You have to tell matlab the type of data it should be expecting in the text file so that it can convert it ‘%d’, ‘%f’, ‘%c’, ‘%s’ Size You can specify how to organize the imported data [m,n] – import the data as m by n, n can be infinite Be careful because matlab will mangle your data and not tell you

Lets Try It Open text1.txt using the fopen command Remember to save the fid, we will need it Create a variable with the data of text1.txt Now create another variable y with the data of text1.txt in it by using fscanf (do not simply copy x) What happens here? Need to set file pointer to beginning using rewind(fid) Now use the size option to import the data with 5 rows and 2 columns Try the same thing with test2.txt It works and fills in the blanks. This is powerful but dangerous

Lets Try It Open text1.txt using the fopen command Remember to save the fid, we will need it fid = fopen('test1.txt) Create a variable with the data of text1.txt [x] = fscanf(fid,'%f%f') Now create another variable y with the data of text1.txt in it by using fscanf (do not simply copy x) [y] = fscanf(fid,'%f%f') What happens here? Need to set file pointer to beginning using frewind(fid)

Lets Try It Now use the size option to import the data with 5 rows and 2 columns [data2] = fscanf(fid,'%f%f',[5,2]) - Careful this is the same format as the original data but not the same organization!! frewind(fid) [data3] = fscanf(fid,'%f%f',[2,5]) data3’ - now the data is formatted correctly Try the same thing with test2.txt It works and fills in the blanks. This is powerful but dangerous

Getting Data fgetl returns the next line of the file as a character array You may need to convert these to numbers >> fid1 = fopen(‘test1.txt’); >> a_str = fgetl(fid1) a_str = 1 2 >> a_num = str2num(a_str) a_num = [1 2]

Realistic File A realistic file of data will have header information, labeled columns and other information embedded within it. See PDXtemp.dat Option 1: Manually go through deleting this information and import using load of fopen commands. Option 2: Have matlab delete and format available data on the fly

Realistic File Powerful function textread can be used to input almost any text file Handles (input variables): Opening the file Ignoring Header Information Accepting Column Labels Will work for most applications

Realistic File Powerful function textread can be used to input almost any text file Usage: [var1 varN] = textread(‘filename’,’format’,args)

Summary Lots of options to load files load for basics fscanf for complex textread for most things xlsread for Excel worksheets Also saving Excel sheets as tab delimitted

Mini-Project Using the textread function, import the full data located in PDXtemp.dat with the stated names and correct data types Take the resulting temperatures in Fahrenheit and convert to Celsius Make use of Matlab help to learn about and implement the textread function Deg_F = 9/5*Deg_C + 32

Mini-Project Solution % Assume that the textfile is saved in the matlab work directory % which is automatically in the path % read data from PDXtemp.txt into 4 variables (assume PDXtemp.txt is in path) [month, high_F, low_F, avg_F] = … textread('PDXtemp.txt','%4c%f%f%f','headerlines', 6); % convert each temperature variable to celcius high_C = (5/9).*(high_F - 32); Low_C = (5/9).*(low_F - 32); avg_C = (5/9).*(avg_F - 32);

Getting Help Help and Documentation Digital Hard Copy Accessible Help from the Matlab Start Menu Updated online help from the Matlab Mathworks website: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.html Matlab command prompt function lookup Built in Demo’s Websites Hard Copy Books, Guides, Reference The Student Edition of Matlab pub. Mathworks Inc.