Вы находитесь на странице: 1из 17

MATLAB Windows

Default View
Command Window main window to enter commands and run programs
Current Folder Window shows MATLAB m-files in the current folder
Workspace Window provides information about currently used variables
Command History Window log of commands entered in the command window

ME280 - Structured Programming

Chapter 1 Slide 1

MATLAB Windows
Additional Windows
Figure Window displays graphs from graphics commands automatically upon execution of the
graphics command
Editor Window provides capability for writing and editing programs and is opened under the
File New Script menu or
button
Help Window provides help information interactively and is accessed via the Help menu or F1

ME280 - Structured Programming

Chapter 1 Slide 2

MATLAB Windows
Configuring MATLAB
Close an individual window within MATLAB by clicking the

in the upper right corner of the window

Desktop Layout menu provides options for opening and closing various MATLAB windows or
combinations of MATLAB windows
Undock a window by clicking

Redock by toggling
corner of the window

and then

in the upper right corner of the window

in the upper right

ME280 - Structured Programming

Chapter 1 Slide 3

Command Window
Entering commands once a command is entered it cannot be modified and reexecuted
>> denotes the command line prompt
Edit commands before they are executed by using backspace, left-arrow, right-arrow and delete keys
Select and edit previous commands entered using the or
Smart recall retrieves previously entered commands based on the first few characters of the
command and the up-arrow key
Tab completion allows automatic completion of a previously defined name based on the first few
letters of the name
Semicolon (;) prevents the value of a command from being written

Commas (,) or semicolons (;) allow several commands on the same line
Ellipse () may be used to continue a long command onto another line
% symbol denotes a comment line that is not executed (documenting)

ME280 - Structured Programming

Chapter 1 Slide 4

Scalar Arithmetic Operators


Mathematical Operators
Addition (3 + 5 or a + b)
Subtraction (7 3 or a b)
Multiplication (6 * 4 or a * b)
Division (2 / 3 = 3 \ 2 or N / D = D \ N)

Exponential ( 5 ^ 3 or a ^ b)

Order of Precedence
Parentheses starting from the innermost

Exponentiation
Multiplication and division
Addition and subtraction

ME280 - Structured Programming

Chapter 1 Slide 5

Answer Display
Format Command - changes the display format of numbers
Style

Result

short (default)

Short fixed decimal format, with 4 digits after the decimal point. If you are displaying a matrix with a
wide range of values, consider using shortG

3.1416

long

Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after
the decimal point for single values.

3.141592653589793

shortE

Short scientific notation, with 4 digits after the decimal point. Integer-valued floating-point numbers
with a maximum of 9 digits do not display in scientific notation.

3.1416e+00

longE

Long scientific notation, with 15 digits after the decimal point for double values, and 7 digits after
the decimal point for single values. Integer-valued floating-point numbers with a maximum of 9
digits do not display in scientific notation.

3.141592653589793e+00

shortG

The more compact of short fixed decimal or scientific notation, with 5 digits.

3.1416

longG

The more compact of long fixed decimal or scientific notation, with 15 digits for double values, and
7 digits for single values.

3.14159265358979

shortEng

Short engineering notation, with 4 digits after the decimal point, and an exponent that is a multiple
of 3.

3.1416e+000

longEng
bank

Example

Long engineering notation, with 15 significant digits, and an exponent that is a multiple of 3.
Currency format, with 2 digits after the decimal point.

ME280 - Structured Programming

3.14159265358979e+000
3.14

Chapter 1 Slide 6

Answer Display
Format Command - changes the display format of line spacing
Style

Result

Example

Suppresses excess line feeds to show more output in a single screen. Contrast with loose.

theta = pi/2
theta =
1.5708

Adds linefeeds to make output more readable. Contrast with compact.

theta = pi/2

compact

loose

theta =
1.5708

ME280 - Structured Programming

Chapter 1 Slide 7

Functions
Mathematical Functions
Absolute value (abs)
Sine (sin or sind), cosine (cos or cosd), tangent (tan or tand), arcsine (asin), arccosine (acos),
arctangent (atan or atan2)
Round (round), ceiling (ceil), floor (floor)
Square root (sqrt)
Natural logarithm (log), logarithm base 10 (log10)
Many others use shift+F1 to browse available functions

General Functions
Clock provides a vector as [year month day hour minute second]
Date provides the current date as dd-mm-yyyy
Calendar provides a calendar for the current month or specified month and year
Many others

ME280 - Structured Programming

Chapter 1 Slide 8

Scalar Number
Constants
pi ()
eps represents the smallest difference between two numbers ( 2.2204e-16)
i or j represents the imaginary number

Indefinite numbers
Infinity is a valid input and is designated by Inf
Division by zero gives a warning and returns Inf

Not-a-Number is an undefined value designated by NaN


Returned by calculations such as 0 / 0

ME280 - Structured Programming

Chapter 1 Slide 9

Variables
Variables - fundamental to programming and are actually pointers to allocated memory space
for storage
Name starts with a letter character
Name may only have letters, underscore, and the digits 0-9
Should not conflict with exiting variables or functions built into MATLAB
It is possible to reassign Inf for example but it may become confusing
Often i or j are reassigned as loop variables when a program does not deal with complex numbers

MATLAB is case sensitive


Assign using the equals operator (a = 2, a = 2 * b, a = sin(pi))
Reassignment in terms of themselves (a = 3 * a - 7, NOT the same as 2a = 7)

Cannot be defined as a MATLAB keyword such as (for, if, else, break, etc.)
If defined as an existing MATLAB function, function cannot be used
ans variable of the last expression calculated not assigned to a variable

ME280 - Structured Programming

Chapter 1 Slide 10

Useful Management Commands


Commands
who - provides a list of variable names in the current workspace
whos provides a list of variables currently in memory and information about their size and type
clear - deletes all the variables stored in memory and shown in the current workspace
clear VarName1 VarName2 etc. removes only the listed variables from memory

clc clears the command window including those of previous sessions


ans - returns the value of the last expression calculated that was not assigned to a variable
Should avoid relying on the ans variable

ME280 - Structured Programming

Chapter 1 Slide 11

Script Files Introduction


Command Window
Executes a single command at a time
Cannot be saved and executed again
Not interactive
Changing the value of a variable in equations previously entered requires reentering all equations
affected by the variable change

Script Files
File with a list of commands that are executed sequentially in the same manner as the command
window

Can be edited and saved as an M-file (*.m) for later use


Any text editor such as Notepad may be used to write a script file
MATLABs Editor Window assist with code development by issuing warnings, providing
autocomplete, and color coded text

ME280 - Structured Programming

Chapter 1 Slide 12

Script Files Use


Creating
Use the MATLAB Editor Window since MATLAB is available to all students and the Editor Window
permits the execution of the script file from within and debugging tools
From MATLAB open the editor window by one of the following:
File New Script menu
New script File button
Ctrl + N

Saving
File name requirements are the same as variable names
From Editor Window save your M-file by one of the following:
File Save as menu
Save File button
Ctrl + S

ME280 - Structured Programming

Chapter 1 Slide 13

Script Files Use


Opening
From MATLAB Desktop or Editor Window
File Open menu
Open File button
Ctrl + O
From the Current Folder Window
Select the appropriate directory for the M-file
Double click the file
Right click the file and select open
Select the file and use Ctrl+D

ME280 - Structured Programming

Chapter 1 Slide 14

Script Files Use


Running
From the MATLAB Editor Window do one of the following:
Debug Run ScriptFileName menu
Run button
F5
From MATLAB Current Folder Window do one of the following:
Right click the appropriate M-file and select Run
Select the desired M-file and press F9
From MATLAB Command Window

Change to the desired M-files directory if necessary


cd C:\FilePath (useful for programs executing subprograms in different directories)
Browse in MATLAB Desktop
Type the name of the M-file to run

ME280 - Structured Programming

Chapter 1 Slide 15

Examples
Problem 1

43 145 30 ln 50

3
5
30

5e 4
6!
ln 90

Problem 2
Define the variables a and b as: a=14, b= 21 then evaluate:

ab
a
sin e a b cos a
a log10 b
b
Problem 3
Define the variables x as x =12 degrees and verify the identity is correct:

1
sin x 3sin x sin 3 x
4
3

ME280 - Structured Programming

Chapter 1 Slide 16

Examples
Problem 4
The prices of an oak tree and a pine tree are $54.95 and $39.95, respectively. Change the display format to
bank, and calculate the following:
(a) the total cost of 16 oak trees and 20 pine trees.
(b) the same as part (a), and add 6.25% sale tax.
(c) the same as part (b) and round the total cost to the nearest dollar.

ME280 - Structured Programming

Chapter 1 Slide 17

Вам также может понравиться