Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.

Slides:



Advertisements
Similar presentations
Introduction to M ATLAB 4 Useful stuff for real programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Advertisements

1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
Some computer fundamentals and jargon Memory: Basic element is a bit – value = 0 or 1 Collection of “n” bits is a “byte” Collection of several bytes is.
M AT L AB Reading & Writing Files. Low-level file input and output is very similar to that in 'C', but with inherent vectorization. Files are opened with.
Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/ :30.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
COMP3221 lec07-numbers-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 7: Number Systems - III
Fall 2006AE6382 Design Computing1 Matlab File & Directory Management Learning Objectives Define file input and output terminology Compare high and low.
2 Systems Architecture, Fifth Edition Chapter Goals Describe numbering systems and their use in data representation Compare and contrast various data.
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.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Simple Data Type Representation and conversion of numbers
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Computer Science 101 Introduction to Programming.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Data Representation.
Number Systems Spring Semester 2013Programming and Data Structure1.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Corporation
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 1 COE1361:
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Lecture 2  Complement  Floating Point Number  Character Encoding.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
CISC105 – General Computer Science Class 9 – 07/03/2006.
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 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647.
Computer Architecture EKT 422
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.
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Today. by 5, by Discuss on Friday. –Homework IV: on web, due following.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Characters and Strings
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Input Output Garbage In, Garbage Out. Outline Announcements: –HWII solutions on web soon –Homework III: due Wednesday Advanced ASCII Binary Basics Cell-arrays.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 11”
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Wednesday Advanced ASCII Binary Basics Filesystem Fun.
Chapter 3 Data Representation
Programming and Data Structure
The Machine Model Memory
Number Representation
ECE Application Programming
Matlab Training Session 4: Control, Flow and Functions
Revision Lecture
EPSII 59:006 Spring 2004.
Other Kinds of Arrays Chapter 11
Data Structures Mohammed Thajeel To the second year students
CS1S467 GUI Programming Lecture 3 Variables 2.
Lecture 13 Input/Output Files.
Matlab Training Session 5: Importing Data
Input/Output Functions
Matlab Training Session 2: Matrix Operations and Relational Operators
Input/Output Functions
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm

Course Outline Term 1 1.Introduction to Matlab and its Interface 2.Fundamentals (Operators) 3.Fundamentals (Flow) 4.Importing Data 5.Functions and M-Files 6.Plotting (2D and 3D) 7.Plotting (2D and 3D) 8.Statistical Tools in Matlab Term 2 9. Term 1 review 10. Loading Binary Data Weeks Topics: Statistics, Creating Gui’s, Exponential curve fitting ….

Week 5 Lecture Outline loading Binary Data A. Week 5 Review – Importing Text Data B. Binary Encoding C. Binary Data Formats D. Exercise

A. Week 5 Review: Importing Text Data Basic issue: –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?

Lots of options to load files –load for basics –fscanf for complex –textread for any text –xlsread for Excel worksheets

load Command opens and imports data from a standard ASCII file into a matlab variable Usage: var_name = load(‘filename’) Restrictions –Data must be constantly sized –Data must be ASCII –No other characters

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’) 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 file_name fid 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 –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 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’ 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

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]

B. Binary Encoding All data files are binary encoded ASCII text format is generally the easiest because it is relatively simple, easy to visualize in a text editor, and is a common output format BUT ASCII text is not the fastest or the most efficient way of encoding data Not all data files are ASCII!

B. Binary Encoding Binary data consists of sequences of 0’s and 1’s Depending on the encoding used, individual meaningful values will occur every 4, 8, 16, 32 or 64 bits For a tutorial on converting between binary and decimal numbers see:

B. Binary Encoding Binary data consists of sequences of 0’s and 1’s Depending on the encoding used, individual meaningful values will occur every 4, 8, 16 or 32 bits

B. Binary Encoding Binary data consists of sequences of 0’s and 1’s Depending on the encoding used, individual meaningful values will occur every 4, 8, 16 or 32 bits

B. Binary Encoding Binary data consists of sequences of 0’s and 1’s Depending on the encoding used, individual meaningful values will occur every 4, 8, 16 or 32 bits

B. Binary Encoding Each group of bits can represent a value, character, delimiter, command, instruction ect. Generally binary data is divided into 8 bit (1 byte) segments = zero = 255 IT IS VERY IMPORTANT TO KNOW WHAT FORMAT THE DATA IS IN BEFORE YOU CAN READ IT!

ASCII ENCODING ASCII: American Standard Code for Information Interchange (1968). ASCII every character is coded by only seven bits of information. The eighth bit is ignored (it can be a zero or one). ASCII consists of 127 characters which include uppercase, lowercase, spaces and formatting characters See for the full ascii tablewww.asciitable.com

ASCII vs Simple Binary Encoding ASCII requires 1 byte to be used for every character Data Table: In ascii 1 byte is used for every character, space and carriage return = 23 bytes If this was encoded in a simple 8 bit binary representation this would only use 11 bytes (1 byte for every number and space)

Binary Precision The number of bits used to represent a value determines how large or small that value can be 8 bits 0 to bits 0 to bits 0 to e+009 Precision also determines how many decimal places can be represented

'schar' Signed character; 8 bits 'uchar' Unsigned character; 8 bits 'int8' Integer; 8 bits 'int16' Integer; 16 bits 'int32' Integer; 32 bits 'int64' Integer; 64 bits 'uint8' Unsigned integer; 8 bits 'uint16' Unsigned integer; 16 bits 'uint32' Unsigned integer; 32 bits 'uint64' Unsigned integer; 64 bits * The first bit denotes the sign if the integer or character is signed. C. Binary Formats: Integers and Characters

Readable Binary Data Formats Floating Point Representation Used for numbers that require decimal representation (real numbers) Established by IEEE (Institute of Electrical and Electronics Engineers ) Encoded in 32 (single precision) or 64 bits (double precision) Single precision(short): 32 bits 1 bit for the sign, 8 bits for the exponent, and 23 bits for the mantissa. Double precision(Long) Real: 64 bits 1 bit for the sign, 11 bits for the exponent, and 52 bits for the mantissa.

Readable Binary Data Formats Floating Point Representation By default matlab stores all values with double precision The functions realmax and realmin return max and min value representations 'float32‘, ‘single’ Floating-point; 32 bits 'float64', 'double' Floating-point; 64 bits

Specifying Machine Formats The computer system used to record or save the binary data in unique addressing orders In order to load binary data from a particular system, Matlab needs to know the machine format You can use the fopen function to determine the machine format [filename, mode, machineformat] = fopen(fid)

Binary File Machine Formats 'ieee-be' or 'b‘: IEEE floating point with big-endian byte ordering 'ieee-le' or 'l' : IEEE floating point with little-endian byte ordering 'ieee-be.l64' or 's‘ : IEEE floating point with big-endian byte ordering and 64-bit long data type 'ieee-le.l64' or 'a‘ : IEEE floating point with little-endian byte ordering and 64-bit long data type 'native' or 'n' : Numeric format of the machine on which MATLAB is running (the default) 'vaxd' or 'd' : VAX D floating point and VAX ordering 'vaxg' or 'g' : VAX G floating point and VAX ordering

Reading Binary Data The function fread() performs all binary data reading in matlab Syntax A = fread(fid) A = fread(fid, count) A = fread(fid, count, precision) A = fread(fid, count, precision, skip) A = fread(fid, count, precision, skip, machineformat) [A, count] = fread(...)

Reading Binary Data Input Arguments: Count: x: read x elements Inf: read to end of file [m,n]: read enough to fill a m by n matrix Precision: Specify input data format eg. Int8, int16, short, long… see previous slides Skip: Skip specified number of bits between segments specified by the Precision argument MachineFormat: Specify machine format 'ieee-be‘, 'ieee-le‘….. See previous slides

Exercise Load and plot position data saved in: week10data.rob This file contains binary position data saved in 32 bit floating point format precision 1.Use the fopen function to determine the machine format hint: [fname, mode, mformat] = fopen(fid) 2. Load the data using the fread function 3. Plot the position 4. Try loading the data with an incorrect argument to see how this changes/corrupts the data

Exercise Solution fid = fopen('week10data.rob','r') %open file for reading %Determine file format [fname, mode, mformat] = fopen(fid) %Format is ieee-le %Read binary data pos_data = fread(fid, inf, 'single', 'ieee-le') plot(pos_data) % plot position data fclose(fid) % close file

Getting Help Help and Documentation Digital 1.Accessible Help from the Matlab Start Menu 2.Updated online help from the Matlab Mathworks website: 3.Matlab command prompt function lookup 4.Built in Demo’s 5.Websites Hard Copy 3.Books, Guides, Reference The Student Edition of Matlab pub. Mathworks Inc.