ch1 - Starting With MATLAB - Tagged
ch1 - Starting With MATLAB - Tagged
Toolstrip
Details Window
Window Purpose
Command Window Main window, enters variables,
runs programs.
Figure Window Contains output from graphic
commands.
Editor Window Creates and debugs script and
function files.
Help Window Provides help information.
Command History Window Logs commands entered in the
Command Window.
Workspace Window Provides information about the
variables that are stored.
Current Folder Window Shows the files in the current
folder.
1.2 WORKING IN THE COMMAND WINDOW
Semicolon (;)
When typed at end of command, suppresses output.
(Only prompt displayed at next line)
Useful for preventing display of large outputs
Used much more in scripts (see Section 1.8)
Percent sign(%)
When typed at beginning of line, MATLAB treats line as
a comment and doesn’t execute line
Used much more in scripts (see Section 1.8)
1.2 WORKING IN THE COMMAND WINDOW
clc command
Clears Command Window display
Up and down arrows still bring back previous
commands
1.3 ARITHMETIC OPERATIONS WITH SCALARS
Precedence order
Same as most calculators
Same as doing arithmetic by hand
For multiple operations of same precedence, MATLAB
goes left to right
Can change order by using parentheses
1.3.2 Using MATLAB as a Calculator
Some types of
formatting
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS
function x
function tan(y/x)
Inputs Outputs y
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS
A function
Has a name
Can have zero or more arguments (inputs)
Can produce zero or more outputs
y = sqrt( x )
EXAMPLE
>> x = 3; ; at end prevents MATLAB from displaying value of x
>> y
y =
30
>> z
z =
900
1.6.1 The Assignment Operator
Can do multiple assignments on one line by separating
with a comma or semicolon. If semicolon, no display for
that assignment
C =(a-B)/2+40-a/B*10
1.6.1 The Assignment Operator
A variable name
Must begin with a letter
Can be up to 63 characters long
Can contain letters, digits, and underscores ( _ )
Can’t contain punctuation, e.g., period, comma,
semicolon
Avoid using the name of a built-in function as the name
of a variable, e.g., don’t call a variable exp or sqrt
1.6.2 Rules About Variable Names
Comment lines
Lines that start with percent sign (%)
Common for first few lines to be comments and to briefly
explain what commands in file do
Editor Window shows comment lines in green
1.8.2 Creating and Saving a Script File
Before MATLAB can run commands in file, you must save
file
If you haven’t named file yet, click on Save icon,
MATLAB brings up Save As dialog box
If you’ve already named and saved file, just click on
Save icon
If you don’t add an extension (.xxx) to the file name,
MATLAB adds “.m”
Rules for file names are same as rules for function
names
Don’t use names of your variables, predefined
variables, MATLAB commands, or MATLAB functions
1.8.3 Running (Executing) a Script File
To execute a script file means to run all of the commands
in it. You can execute a file by
Pressing the Run icon (a green arrow)
Typing the file name in the Command Window and
pressing ENTER
MATLAB will execute file if it is in MATLAB’s current
folder or if the file’s folder is in the search path (explained
next)
1.8.4 Current Folder