MATLAB TUTORIAL 2023
MATLAB TUTORIAL 2023
1.BASICS
1. MATLAB statements are
expressions or assignments,
variable declarations ,
operations with variables,
input- or output-instructions,
control flow statements or function calls,
that perform specific tasks (plots, mathematical
computations etc.).
1
1.B)INTERACTIVE MODE ="Hello World" Program in MATLAB
A "Hello World" program is the smallest executable program in
a programming language and prints the text "Hello World" to
default output (here: the command window).
We do it first in interactive mode and then in a script.
1.C)SCRIPT MODE
In script mode, we first create a new script with the name
"test_hello.m", then type the MATLAB statements below in the
script file and save it, and finally execute the script by clicking the
"RUN"-Button.
Code Explanation
The code starts in line 1 with a comment (indicated by "%"-
symbol), that states the name of the script file.
2
Note that the following lines are also commented. In lines 2 and
3 we insert code for resetting / preparing workspace and
command window.
In line 4 we create a new variable "text" and assign it the value
'Hello world'.
In line 5 we print the string 'Hello World' using disp().
% Script: test_hello.m
clear;clc; % Clear workspace and command window
format compact % Compactify command window output
text = 'Hello World'
disp('Hello World!');
1.D)COMMENTS
Comments in MATLAB start with a "%"-symbol followed by text.
Comments are used for documentation purpose, they are not
executed by the system.
Two percent-symbols "%%" mark a section.
Sections can be executed separately using the RUN SECTION-
Button.
3
%% Section 1
% Comment 1: This is the first comment for section 1.
text = 'Let''s start simple' % Here we assign a value to the
variable named text
%% Section 2
% Comment 2: This is the first comment for section 2.
a = 10 % Here we assign a value to variable a
b = 20 % Here we assign a value to variable b
4
% test_variables.m
a = 10; b = 20.5;
sum = a + b
s1 = "apples";s2 = "pears";
text1 = s1 + "+" + s2
text2 = replace(text1, '+',' or ')
1.F)DATA TYPES
MATLAB supports different data types, that are represented by
classes:
Numerical data types: int, single, double
Boolean data types: logical
String data types: string, char
In order to find out the exact data type and size of your variables,
use the commands whos, class. With whos, you can find out size
and type of workspace objects. With class, you can find out the
data type of a variable.
Code Explanation : We declare two variables a and b by
assigning numeric values, 10 and 20.5 respectively. Then show
information about them using whos.
a = 10;
b = 20.5;
whos a
whos b
5
1.G)STRING VARIABLES
String variables in MATLAB can be either of class "string" or of
class "character array".
When you create a string using single quotes, it is stored as
character array.
When you create a string using double quotes, it is stored as a
string.
A character array is a sequence of characters, just as a numeric
array is a sequence of numbers.
The String class provide a set of functions for working with text
as data.
text1 = "Hello"
whos text1
text2 = 'from Kaiserslautern'
whos text2
6
There are three ways to output variable values to command
window.
Type the name of a variable in a new line, with no terminating
semicolon.
In an assignment statement, omit semicolon at the end of a
statement.
The content of the variable then is displayed in the command
window.
Use disp()-function. This function takes as argument a variable
/ vector / matrix and displays it, omitting the name of the
variable.
Use fprintf()-function. This function builds a formatted output
by using placeholders for the variables to be inserted.
In our example, the value of variable a will be inserted in the
place indicated by the first "%d", the value of variable b will be
inserted in the place indicated by the second "%d" etc.
Example : calculate the sum of two variables and generate
output in the command window.
% test_output.m
a = 10; b = 20; sum = a + b;
% (1) No semicolon
disp('(1) Output by typing variable name')
sum % output: sum = 30
% (2) Use disp
disp('(2) Output using disp()')
disp([a b sum]) % output: 10 20 30
% (3) Use fprintf for formatted output
disp('(3) Output using fprintf')
% output: a = 10, b = 20, Sum = 30
fprintf('a = %d, b = %d, Sum = %d\n', a, b, sum)
7
2.B)INPUT WITH input()
When presenting a MATLAB script to non-technical users, it
may be helpful to let them enter configuration parameters as
input from the command window or an input dialog.
Input entered in the command window can be stored in
variables using the input()-function.
Example : Input using prompt.
% Display a prompting text
prompt = 'Enter a: ';
% Read input
a = input(prompt);
prompt = 'Enter b: ';
b = input(prompt);
sum = a + b;
fprintf("Sum = %.2f\n", sum);
2.C)INPUT DIALOG
An input dialog as shown below is created using the MATLAB-
function inputdlg. We pass four arguments to the function:
prompt -- the labels of the input fields
dlgtitle -- the title of the dialog
windowsize -- the size of the window
definput -- specifies the default value for each edit field.
8
The definput argument must contain the same number of
elements as prompt.
The function returns an array of strings "answer", that contains
the values entered in the edit fields.
Example :Input dialog for two variables.
prompt = {'Enter a:', 'Enter b:'};
dlgtitle = 'Input';
windowsize = [1 50];
definput = {'10','20'};
answer = inputdlg(prompt, dlgtitle, windowsize, definput)
a = str2num(answer{1})
b = str2num(answer{2})
9
7. Also, it is important to use the correct delimiters for the
decimal point, in a computer with German settings, this will be
comma (,), else dot (.).
11