Quick Guide to MATLAB® Commands and Syntax
Input Statements:
% Prompt the user to enter a number
x = input('Enter a number please: ');
% Prompt the user to enter a numerical array
v = input('Enter your array values in square brackets: ');
% Prompt the user to enter text (a string)
month = input('Enter the month that you were born: ','s');
% Create a pop-up menu with directions & three buttons: Yes,No,What?
choice = menu('Do you enjoy programming','Yes','No','What?');
Output Statements:
% Decimal Numbers
x = 75.176;
fprintf('The variable x = %0.2f \n',x);
% %0.2f means show two places behind decimal point
The variable x = 75.18
% Integers
y = 750;
fprintf('The value of y is: %i \n',y);
The value of y is: 750
% Strings
course = 'Calculus II';
fprintf('The name of this course is: %s \n',course);
The name of this course is: Calculus II
% Mixture
x = 75.176; y = 750; course = 'Calculus II';
fprintf('x = %0.1f, y = %i, course is %s \n',x,y,course');
x = 75.2, y = 750, course is Calculus II
Arithmetic, Comparison, and Logical Operators:
Arithmetic Operators Comparison Operators Logical Operators
Addition + Equal == Scalar And &&
Subtraction − Not Equal ~= Scalar Or ||
Multiplication * Less Than < Not ~
Division / Greater Than > Array And &
Power ^ Less Than or Equal To <= Array OR |
Modulo mod(a,b) Greater Than or Equal To >=
% Strings
month = 'July';
strcmp(month,'July'); % True
strcmp(month,'August'); % False
strcmp(month,'july'); % False (MATLAB is Case Sensitive!)
Conditional Statements:
if Condition1
% MATLAB Commands to execute if Condition1 is TRUE
elseif Condition2
% MATLAB Commands to execute if Condition1 is FALSE
% and Condition2 is TRUE
elseif Condition3
% MATLAB Commands to execute if Condition1 is FALSE
% and Condition2 is FALSE
% and Condition3 is TRUE
else
% MATLAB Commands to execute if Conditions 1-3 are FALSE
end
For Loops:
Assume variable N is defined as an integer
for k = 1:N
% MATLAB Commands to execute a total of N times
% Counter index, k, starts at 1 and increments to N
end
Another Option
for k = 1:3:12
% MATLAB Commands
% Counter index, k, loops through the values 1, 4, 7, 10
end
While Loops:
while Condition
% MATLAB Commands will execute again and again as long as
% Condition is true
end
Arrays:
Note: in MATLAB, the first entry in an array is indexed as 1.
This is different from Python where the first entry in a list or
array is indexed as 0!
Suppose x is a vector (1-d Array)
x(1) %1st entry of vector x
x(2:4) %2nd , 3rd, and 4th entries of vector x
x(3) = 5 % Puts a 5 in the 3rd entry of vector x
N = length(x) % N = the number of entries in vector x
Suppose M is a matrix (2-d Array)
M(1,4) is the entry in the 1st row and 4th column of M
M(2,3) = 5 % Puts a 5 in 2nd row and 3rd column of M
[Rows Cols] = size(M) % Rows = number of rows in M
% Cols = number of columns in M
Plot:
x = -pi:pi/100:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,'-k','LineWidth',2)% plot x and y using line,
%color black (k) and
%thickness 2
subplot(m,n,p) breaks the Figure window into an m-by-n matrix of
small axes, selects the p-th axes for the current plot
Functions:
Execute the Function at the Command Prompt or from another MATLAB program:
>> [Area,Perimeter]=Rectangle(3,4)
Area = 12
Perimeter = 14
Note: if you call the function from another function, both functions must be in the same folder.
Reading and Writing to Files:
Open a File:
fid = fopen(‘filename.txt’,PERMISSION)
PERMISSION:
'r' open file for reading
'w' open file for writing; discard existing contents
'w+' open or create file for reading and writing;
discard existing contents
Read from File:
A = fscanf(fid,formatSpec,sizeA)
Write to File:
fprintf(fid, 'The value of pi is: %0.3f \n',pi)
Close the File:
fclose(fid)