1. Basic Data Manipulation

Download Report

Transcript 1. Basic Data Manipulation

Getting Started
with MATLAB (part2)
1.
2.
Basic Data manipulation
Basic Data Understanding
1.
2.
3.
The Binary System
The ASCII Table
Creating Good Variables
1
1. Basic Data Manipulation

New vocabulary:




Variables
Assignment
Storing values in the computer
It is assumed that these terms are understood:




Input
Output
Programmer
User
1. Basic Data Manipulation

Starting MATLAB on every machine usually leads to the prompt
symbols >> in the command window
3
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.
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:
5
1. Basic Data Manipulation
Assign values to
variables z and y
6
1. Basic Data Manipulation
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.
7
1. Basic Data Manipulation
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.
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.
8
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
9
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!
2.1 The Binary System
Base 10
0
1
2
3
4
5
6
Base 2
02
12
102
112
1002
1012
1102
2.1 The Binary System

How does
10
represent a 2 ?
12
2.2. The ASCII table

Group the 0’s and 1’s to represent what you want

Done automatically!
13
2.2. The ASCII table

For example: The symbol 2 in decimal is represented
using the sequence 0000 0010 in binary.
14
2.2. The ASCII table

Even for characters…
15
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>
What is the expected outcome?
radius = ‘radius of a circle’ <enter>
radius + 1 <enter>
What
is the un-expected outcome?
16
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!)
17
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
18
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.
19
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
20