MATLAB Development environment
Getting Started with MATLAB
Calculations at Command Line
Using MATLAB Functions
MATLAB Desktop Environment
Getting MATLAB Help
MATLAB Development Environment Introduction - 2
Getting Started with MATLAB
MATLAB Development Environment Introduction - 3
Getting Started with MATLAB
MATLAB Development Environment Introduction - 4
Quitting MATLAB
• To end your MATLAB
session, select Exit
MATLAB from the File
menu in the desktop
OR
• Using the close button
OR
• Type quit in the Command
Window.
MATLAB Development Environment Introduction - 5
Calculations at the command line
MATLAB as a calculator Assigning Variables
>> -5 / ( 4.8 + 5.32 ) ^ 2 >> a = 2; Semicolon suppresses
ans = >> b = 5; screen output
-0.048821
>> a ^ b Results assigned to
>> ( 3 + 4 i ) * ( 3 – 4 i ) ans =
ans = “ans” if name not given
32
25
>> cos (pi/2) >> x = 5/2*pi;
ans = >> y = sin (x)
6.1232e-017 y=
>> exp ( acos (0.3) ) 1 Use parentheses ( )
ans = >> z = asin (y)
3.547 z=
for function inputs
Numbers are stored in double-precision floating point format
MATLAB Development Environment Introduction - 6
Executing a series of Commands
MATLAB Development Environment Introduction - 7
Script M-files
• Standard ASCII text files
• Contain a series of MATLAB expressions
(Typed as you would at the command line)
• Commands parsed & executed in order
% Comments start with "%" character
pause % Suspend execution - hit any key to continue.
keyboard % Pause & return control to command line.
% Type "return" to continue.
break % Terminate execution of current loop/file.
return% Exit current function
% Return to invoking function/command line.
MATLAB Development Environment Introduction - 8
Script M-Files (Example)
• Write a script file to generate first ‘n’ elements of fibonacci
series
>> fibo
enter the number of elements to be generated 8
1 1 2 3 5 8 13 21
MATLAB Development Environment Introduction - 9
The MATLAB Workspace
A statement like
z = 10;
Creates a variable named z, stores the value 10 in it, and saves it
in a part of computer memory known as the workspace.
A workspace is the collection of all the variables and arrays that
can be used by MATLAB when a particular command, M-file, or
function is executing.
The MATLAB Command window and script files have a common
workspace
MATLAB Development Environment Introduction - 10
The MATLAB Workspace
To Clear a variable from the workspace the ‘clear’ command can
be used as shown below.
>> a = 10;
>> b = 20; Clear variable_name clears the specified
variable from the workspace
>> c = a + b ;
>> who
clear clears all the variables from the
Your variables are
workspace
abc
>> clear c;
>> who
Your variables are
ab
MATLAB Development Environment Introduction - 11
The MATLAB Workspace
A list of the variables and arrays in the current workspace can be
generated with the whos and who commands.
For example, after M-files calc_area is executed, the workspace
contains the following variables:
>> whos
Name Size Bytes Class
area 1x1 8 double array
radius 1x1 8 double array
s 1x32 64 char array
Grand total is 34 elements using 80 bytes
>> who
Your variables are:
area radius s
MATLAB Development Environment Introduction - 12
Working with Files & Variables
MATLAB Function Usage
CD/PWD, DIR Navigating directories
WHAT Displays files in a directory (grouped by type)
! (bang) Invoke operating system
WHICH Identifies the object referenced
CLEAR Remove function or variable from memory
WHO, WHOS List workspace variables
SIZE Returns the size of a matrix (rows and columns)
MATLAB Development Environment Introduction - 13
some useful functions used often in MATLAB
>> pwd
ans =
C:\Program Files\MATLAB\R2007a\work
>> cd ('C:\Program Files\MATLAB\R2007a')
>> pwd
ans =
C:\Program Files\MATLAB\R2007a
>> dir
M-files in the current directory
. .. adplin.m [Link] wave.m
>> what
M-files in the current directory
C:\Program Files\MATLAB\R2007a
adplin wave
MATLAB Development Environment Introduction - 14
some useful functions used often in MATLAB
>> which mean
C:\Program Files\MATLAB\R2007a\toolbox\matlab\datafun\mean.m
>> a=[1 2 3;4 5 6] >> ndims(a)
a= ans =
1 2 3 2
4 5 6
>> size( a )
ans =
2 3
>> length(a)
ans =
3
MATLAB Development Environment Introduction - 15
Array Editor
For editing 2-D
numeric arrays
double-click
>> openvar A
MATLAB Development Environment Introduction - 16
Online Help
• The help command >> help
• The help window >> helpwin
>> help cd
CD Change current working directory.
CD directory-spec sets the current directory to the one specified.
CD .. moves to the directory above the current one.
CD, by itself, prints out the current directory.
WD = CD returns the current directory as a string.
Use the functional form of CD, such as CD('directory-spec'),
when the directory specification is stored in a string.
See also PWD.
MATLAB Development Environment Introduction - 17
More About Getting Help
• The HELP command >> help
• The help window >> helpwin
• MATLAB Documentation
• The MATLAB Help Browser >> helpbrowser
• Online Reference (HTML / PDF) >> doc
• Solution Search Engine
• Index / Contents
• Link to The Mathworks
• FTP site & latest documentation
• Submit Questions, Bugs & Requests
MATLAB Development Environment Introduction - 18
Which Help Do I Use?
• If there is a type (grouping) of functionality that
you are looking for
>> helpwin
• If you are looking for a specific functionality
Help->Search->Full Text Search
• If you know the function name
>> help function_name
or
>> doc function_name
MATLAB Development Environment Introduction - 19
MATLAB Editor/Debugger
>> edit %opens new file OR brings editor into focus
>> edit <filename> %opens file named <filename>
>> edit startup %opens the startup.m file (shown)
MATLAB Development Introduction - 20
Environment
Profiler
MATLAB includes a graphical user interface, the Profiler, to
help you improve the performance of your M-files.
Type profile viewer to open the GUI
Enter statement to profile
Click Start
MATLAB Development Introduction - 21
Environment
MATLAB Desktop
MATLAB Current Directory
MATLAB Help Window
MATLAB Command History
MATLAB Workspace