Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Types and Variables. Computer Programming 2 C++ in one page!
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
General Computer Science for Engineers CISC 106 Lecture 22 Dr. John Cavazos Computer and Information Sciences 04/13/2009.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Computer Science 210 Computer Organization Floating Point Representation.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
Simple Data Type Representation and conversion of numbers
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Input & Output: Console
Numeric precision in SAS. Two aspects of numeric data in SAS The first is how numeric data are stored (how a number is represented in the computer). –
Computer Science 111 Fundamentals of Programming I Number Systems.
Computer Architecture
Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output.
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.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
Data Representation - Part II. Characters A variable may not be a non-numerical type Character is the most common non- numerical type in a programming.
Floating Point Representations CDA 3101 Discussion Session 02.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Fundamental Programming: Fundamental Programming Introduction to C++
Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes –
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3.
Asking the USER for values to use in a software 1 Input.
Asking the USER for values to use in a software 1 Input.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Data Representation: Floating Point for Real Numbers Computer Organization and Assembly Language: Module 11.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Variables Symbol representing a place to store information
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
IT11004: Data Representation and Organization Floating Point Representation.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
C is a high level language (HLL)
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Computer Science 210 Computer Organization
How Computers Store Variables
TMF1414 Introduction to Programming
Data Structures Mohammed Thajeel To the second year students
Chapter 2.
IDENTIFIERS CSC 111.
Computer Science 210 Computer Organization
C++ Data Types Data Type
Coding Concepts (Data- Types)
Presentation transcript:

Matlab Data types, input and output

Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64 single double Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64 single double

uint8 The uint8 Data Type The uint8 Data Type uints are a type of integer. The u-int-8 stands for: U: uints are a type of integer. The u-int-8 stands for: U: Means: Unsigned Means: Unsigned Which means: All values are positive (no negative "sign" allowed). Which means: All values are positive (no negative "sign" allowed). INT: INT: Means: Integers Means: Integers Which means: All values are whole numbers, 0,1,2,3... Which means: All values are whole numbers, 0,1,2, Only 8 bits of information: Only 8 bits of information: Which means: the max value is 255 and the min value is 0. Which means: the max value is 255 and the min value is 0.

Single and Double precision Double-Precision Floating Point MATLAB constructs the double-precision (or double) data type according to IEEE® Standard 754 for double precision. Any value stored as a double requires 64 bits, formatted as shown in the table below: Double-Precision Floating Point MATLAB constructs the double-precision (or double) data type according to IEEE® Standard 754 for double precision. Any value stored as a double requires 64 bits, formatted as shown in the table below: Bits Usage 63 Sign (0 = positive, 1 = negative) 62 to 52 Exponent, biased by to 0 Fraction f of the number 1.f Bits Usage 63 Sign (0 = positive, 1 = negative) 62 to 52 Exponent, biased by to 0 Fraction f of the number 1.f Single-Precision Floating Point Single-Precision Floating Point MATLAB constructs the single-precision (or single) data type according to IEEE Standard 754 for single precision. Any value stored as a single requires 32 bits, formatted as shown in the table below: MATLAB constructs the single-precision (or single) data type according to IEEE Standard 754 for single precision. Any value stored as a single requires 32 bits, formatted as shown in the table below: Bits Usage 31 Sign (0 = positive, 1 = negative) 30 to 23 Exponent, biased by to 0 Fraction f of the number 1.f Bits Usage 31 Sign (0 = positive, 1 = negative) 30 to 23 Exponent, biased by to 0 Fraction f of the number 1.f

uint8 The uint8 Data Type The uint8 Data Type uints are a type of integer. The u-int-8 stands for: U: uints are a type of integer. The u-int-8 stands for: U: Means: Unsigned Means: Unsigned Which means: All values are positive (no negative "sign" allowed). Which means: All values are positive (no negative "sign" allowed). INT: INT: Means: Integers Means: Integers Which means: All values are whole numbers, 0,1,2,3... Which means: All values are whole numbers, 0,1,2, Only 8 bits of information: Only 8 bits of information: Which means: the max value is 255 and the min value is 0. Which means: the max value is 255 and the min value is 0.

uint16 The uint16 Data Type The uint16 Data Type uints are a type of integer. The u-int-16 stands for: U: uints are a type of integer. The u-int-16 stands for: U: Means: Unsigned Means: Unsigned Which means: All values are positive (no negative "sign" allowed). Which means: All values are positive (no negative "sign" allowed). INT: INT: Means: Integers Means: Integers Which means: All values are whole numbers, 0,1,2,3... Which means: All values are whole numbers, 0,1,2, Only 16 bits of information: Only 16 bits of information: Which means: the max value is and the min value is 0. Which means: the max value is and the min value is 0.

uint32 The uint32 Data Type The uint32 Data Type uints are a type of integer. The u-int-32 stands for: U: uints are a type of integer. The u-int-32 stands for: U: Means: Unsigned Means: Unsigned Which means: All values are positive (no negative "sign" allowed). Which means: All values are positive (no negative "sign" allowed). INT: INT: Means: Integers Means: Integers Which means: All values are whole numbers, 0,1,2,3... Which means: All values are whole numbers, 0,1,2, Only 32 bits of information: Only 32 bits of information: Which means: the max value is and the min value is 0. Which means: the max value is and the min value is 0.

int8 The int8 Data Type The int8 Data Type ints are a type of integer. The int-8 stands for signed integers. ints are a type of integer. The int-8 stands for signed integers. INT: only integers INT: only integers Only 8 bits of information: (using the 2’s complement technique) Only 8 bits of information: (using the 2’s complement technique) Which means: the max value is 127 and the min value is Which means: the max value is 127 and the min value is -128.

int8 The int8 Data Type The int8 Data Type ints are a type of integer. The int-8 stands for signed integers. ints are a type of integer. The int-8 stands for signed integers. INT: only integers INT: only integers Only 8 bits of information: (using the 2’s complement technique) Only 8 bits of information: (using the 2’s complement technique) Which means: the max value is 127 and the min value is Which means: the max value is 127 and the min value is -128.

int16 The int16 Data Type The int16 Data Type ints are a type of integer. The int-19 stands for signed integers. ints are a type of integer. The int-19 stands for signed integers. INT: only integers INT: only integers Only 16 bits of information: (using the 2’s complement technique) Only 16 bits of information: (using the 2’s complement technique) Which means: the max value is and the min value is Which means: the max value is and the min value is

char char char character data stored in 2 bytes (16 bits) A = ‘a’ Strings are stored as a vector of characters B=‘abc’ C = char(123)

Single and double precision single: single precision numerical data (32 bits in Matlab) A = single(pi); double: double precision numerical data (64 bits in Matlab). By default, data are stored as a double precision data in Matlab. B = double(pi)

Number formatting Matlab will often display numbers with a given number of decimals based on "formatting" options as opposed to their actual value. Matlab will often display numbers with a given number of decimals based on "formatting" options as opposed to their actual value. Note: While I will differentiate between integers and doubles, Matlab will treat most numbers (unless it is explicitly told otherwise) as doubles. Note: While I will differentiate between integers and doubles, Matlab will treat most numbers (unless it is explicitly told otherwise) as doubles. This is because, from a scientific viewpoint, doubles are the most accurate representation the computer can (typically) use. We will talk about the limits of precision on a computer at another time. This is because, from a scientific viewpoint, doubles are the most accurate representation the computer can (typically) use. We will talk about the limits of precision on a computer at another time.

Data type conversion A = ‘a’ B = int8 (A) C = pi D = char(C) E = single (D) D = double(D)

Formatting numbers You can change this behavior by using the "format" command >> format long >> a a = >> format short >> a a = You can change this behavior by using the "format" command >> format long >> a a = >> format short >> a a =

fprintf function (not covered) The fprintf function allows you to "write" information to the screen for the user to view. This very important when user interaction is involved. The 'f' in printf stands for formatted. This means you can "format" how the data is printed in such a manner as to make it easy to read. The fprintf function allows you to "write" information to the screen for the user to view. This very important when user interaction is involved. The 'f' in printf stands for formatted. This means you can "format" how the data is printed in such a manner as to make it easy to read.

Print information The display command: disp is a function that will display a value The display command: disp is a function that will display a value name = 'Jim'; name = 'Jim'; disp(name); disp(name);

Input function The input function allows you to ask a user to type some sort of information into the program and to save that information into a variable that the program can process. The input function allows you to ask a user to type some sort of information into the program and to save that information into a variable that the program can process. Ask the user a question and wait for a typed response. Save the response in a variable: Ask the user a question and wait for a typed response. Save the response in a variable: age = input('how old are you: '); % At this point, the variable: age, will contain % whatever value the user types age = input('how old are you: '); % At this point, the variable: age, will contain % whatever value the user types

Reading strings By default, the input function reads a number, and if you type say, hello it will assume that hello is a variable containing a number. If you really want the string "hello" then you would have to type 'hello' (tick marks). By default, the input function reads a number, and if you type say, hello it will assume that hello is a variable containing a number. If you really want the string "hello" then you would have to type 'hello' (tick marks). To tell a Matlab program to read a string of characters directly with out having to type the tick marks, you must use the 's' syntax as shown here: To tell a Matlab program to read a string of characters directly with out having to type the tick marks, you must use the 's' syntax as shown here: >>name = input('what is your name: ', 's'); % user types jim without tick marks name = jim % At this point, the variable: name, will contain % whatever value the user types (as a string of characters), % in this case 'jim' >>name = input('what is your name: ', 's'); % user types jim without tick marks name = jim % At this point, the variable: name, will contain % whatever value the user types (as a string of characters), % in this case 'jim'

Reading strings >>name = input('what is your name: '); % user types jim without tick marks ??? Error using ==> input Undefined function or variable 'jim'. >>name = input('what is your name: '); % user types 'jim' with tick marks name = jim >>name = input('what is your name: '); % user types jim without tick marks ??? Error using ==> input Undefined function or variable 'jim'. >>name = input('what is your name: '); % user types 'jim' with tick marks name = jim