Professional Documents
Culture Documents
Introduction To Matlab
Introduction To Matlab
to MATLAB
2017.02. 송지원
01 Introduction
CONTENTS 1
2
What is MATLAB?
Getting started
3 Editor
4 MATLAB Help
02 MATLAB Basics
1 What are matrices?
2 Vectors and matrices
3 Colon operator
4 Vector and matrix indexing
5 Deleting rows and columns
05 Programming in MATLAB
1 If statement
2 Switch statement
3 For loop
4 While loop
06 Plotting
1 Plot
2 Color, style and marker
3 Multiple plots
4 Subplots
01
Introduction 1 What is MATLAB?
• Application Development
• The MATLAB editor can be used to create and edit M–files, in which you can write and save
MATLAB programs.
• A m-file can take the form of a script file or a function.
• A script file contains a sequence of MATLAB statements; the statements contained in a script file
can be run in the specified order, in the MATLAB command window simply by typing the name of
the file at the command prompt
Workspace
Current Directory Editor
• HELP: “help plot” displays a description of and syntax for the function plot in the Command
Window
• DOC: “doc plot” displays the help browser for the MATLAB function plot. You can invoke the
MATLAB help browser by typing ”helpbrowser” at the MATLAB command prompt, clicking on the
help button, or by selecting Start → MATLAB → Help from the MATLAB desktop.
02
MATLAB Basics 1 What are matrices?
• 3 variables: a, B and C
• a is a scalar: one-‐ dimensional array with one element
• B is a vector: one-‐ dimensional array with length 7
• C is a matrix: a two-‐dimensional array
• Scalars, vectors and matrices are all arrays
• MATLAB prefers operations on arrays instead of scalars
02
MATLAB Basics 2 Vectors and matrices
• The examples below illustrate how vectors and matrices can be created in MATLAB .
•
• ex
▷ Concatenating matrices
• Concatenating matrices are straightforward MATLAB as long as their dimensions are consistent
• ex
>> A = [1, 2, 3]; B = [4, 5, 6];
>> C = [A, B]
C =
1 2 3 4 5 6
>> C = [A; B]
C =
1 2 3
4 5 6
02
MATLAB Basics 3 Colon operator
• The colon operator( : ) allows you to create vectors with a sequence of values from the
start value to the stop value with a specified increment value
• ex >> A = [-2: 2]
A =
-2 -1 0 1 2
• The colon operator can also be used to access the whole or a set consecutive elements within
a dimension of a matrix.
• ex >> C = [1 2 3; 4 5 6; 7 8 9]
C =
1 2 3
4 5 6
7 8 9
>> C( :,3 ) >> C( 2:3, 2:3 )
ans = ans =
3 5 6
6 8 9
9
02
MATLAB Basics 4 Vector and matrix indexing
• But if you delete a subscript, the remaining elements will become a row vector
– A(1) = []; %will work
03
Basic data types and Operations 1 Basic data types
single(x)
03
Basic data types and Operations 1 Basic data types
• Cells : Array of indexed cells, each capable of storing array of different dimension and data type
• Structure : C-‐like structure, named fields capable of storing array of different dimension and
data type
• Only use primary alphabetic characters (i.e., ”A-Z”), numbers, and the underscore character in
variable names.
• No spaces in variable names.
• MATLAB is case sensitive. The same text, with different combinations of capital and small case
letters, will not be interpreted the same in MATLAB .
For example, ”VaRIAbLe”, ”variable”, ”VARIABLE” and ”variablE” would all be considered distinct
variables in MATLAB .
03
Basic data types and Operations 3 Arithmetic operators
•
+ Addition
- Subtraction
* Multiplication
/ Division
\ Left division
^ Power
• Examples x + y/ x * y/ x^2
03
Basic data types and Operations 4 Relational operators
•
< Less than
== Equal to
~= Not equal to
• Examples x < y / A == B/
04
MATLAB Functions 1 M-files
• Scripts work like an extended command line statement. All variables are shared between the
workspace and the script. Scripts have no input or output arguments.
• Functions have their own workspace. They are usually called with one or several input
arguments and return one or several output arguments. They are declared by the keyword
function.
04
MATLAB Functions 2 Simple Built-in Functions
p = a*b;
Saved under the file name product.m. It can be called from the command line.
>> x = product(3,4)
x =
12
05
Programming in MATLAB 1 If statement
• ex grade = 'B';
switch(grade)
case 'A'
fprintf('Excellent!\n' );
case 'B'
fprintf('Well done\n' );
case 'C'
fprintf(‘You passed\n' );
otherwise
fprintf('Invalid grade\n' );
end
05
Programming in MATLAB 3 For loop
• Syntax
for variable = vector
statements
end
05
Programming in MATLAB 3 For loop
• ex for ii = 1:10
for jj = 1:10
A(ii,jj) = ii / jj;
end
end
05
Programming in MATLAB 4 While loop
• Syntax
while condition
statements
end
05
Programming in MATLAB 3 While loop
• ex ii = 1;
A(ii) = ii/10000;
ii = ii + 1;
end
06
Plotting 1 Plot
▷ plot
• The most basic plotting command is plot !
– plotting the values of a vector versus the index of the elements : plot(y)
– plotting the values of a vector versus the values of another vector : plot(x,y)
• ex >> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
06
Plotting 1 Plot
• ex >>xlabel(‘x’)
>>ylabel(‘y=cos(x)’)
• It is possible to specify color, line styles, and markers when you plot your data using the plot
command: plot(x,y,’color_style_marker’)
•
Type Values Meanings
Color ‘r’ red
‘g’ green
‘b’ blue
Line style ‘-’ solid
‘- -’ dashed
‘:’ dotted
Marker ‘+’ plus mark
‘s’ filled square
06
Plotting 3 Multiple plot
• subplot (M, N, P)
: There will be M rows and N columns of subfigures and MATLAB will place the result of the
next ”plot” command in the Pth subfigure
• ex >> X = [1 3 4 6 8 12 18];
>> Y1 = 3*X;
>> Y2 = 4*X+5;
>> subplot(2,1,1)
>> plot(X,Y1)
>> subplot(2,1,2)
>> plot(X,Y2)