getting started with matlab (part2) 1. basic data manipulation 2. basic data understanding 1. the...

21
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables 1

Upload: martha-darlene-cunningham

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 4: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

1. Basic Data Manipulation

A variable is a name given for a memory location “Assigning a value to a variable” means to place a value

in the memory location associated with the variable name

Matlab usually shows what the variables are - in the Command Window, but also in the Workspace.

Page 5: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

5

1. Basic Data Manipulation

How about solving equations?

Normal algebra ≠ programming Assume the following mathematic equation:

z = x + y

In algebra: when z is 10 and y is 7, what is x equal to?

z = x + y

10 = x + 7

Solve for x, obtain _____ Look at Matlab:

Page 8: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

8

1. Basic Data Manipulation

In Matlab, all the known variables must be on the right side of the equal sign before executing the command.

At any time, only one variable can be on the left.

Assign values to variables z and y

Once x is on the left side, and all known variables (z and y) are on the right, Matlab executes the command.

Page 9: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

9

2. Basic Data Understanding

How exactly is the data stored in the memory? binary (i.e. machine) language: 0 and 1’s

How is the number 2 represented then?

Remember that the symbol we see is NOT the value represented by it.

For example: what number do these represent?

V |||| 5

Page 10: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

2.1 The Binary System

As shown on the previous slide, a number can be represented using different symbols. In fact, any set of symbols can be created to represent numbers.

In the “binary” system, there are only two symbols:

0 and 1

With just these two symbols, a computer can represent any number we want!

Page 16: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

16

The limit between calculator and programming software Although Matlab looks like a calculator, everything is actually stored

following the ASCII chart. Be careful: Matlab will execute mathematical operations on characters.

What does this mean?

For example, in the command window, Do It Yourself (2.3) radius = 49 <enter> radius + 1 <enter>

radius = ‘radius of a circle’ <enter> radius + 1 <enter>

What is the expected outcome?

What is the un-expected outcome?

Page 18: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

18

3. Creating Good Variables

This last example should make you realize the importance of good variable names: those that describe their content.

Do not name a variable radius when it will contain a name, or a sentence, prefer title_radius in the example above.

In the long term, it will save you time debugging (fixing errors!)

Page 19: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

19

3. RULES not to break!

To create a variable in Matlab, simply assign it a value.

When coming up with a name, follow these RULES: It cannot contain spaces: Use the underscore, or

capitalizeTheFirstLetterOfAdditionalWord It cannot start with a digit. However, it can have digits in the rest

of it. It cannot contain any special characters (other than the

underscore of course!) It should not be any keywords Matlab knows, or It should not be the filename of any saved files

Page 20: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

20

3. Good HABITS, strongly encouraged!!!

Good Programming Habits applied in EGR115 It must represent its content NO one letter long variables, except for loop counters: v, t, p, x Keep it short, it will avoid typos later on If you can, include a unit IN MATLAB: lower case i (Square root of -1) and j already have

values. Avoid reusing them.

Page 21: Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables

Wrapping Up

Basic Data Manipulation Programming IS NOT algebra ALL known values must be on the right hand side of the = ONE unknown must be on the left hand side of the =

Basic Data Understanding The binary system is really how all computers store data All is related to the ASCII table: both numbers and letters Matlab will execute mathematical operations on anything:

numbers AND letters Naming variables properly can prevent a lot of errors

21