0% found this document useful (0 votes)
13 views

L2 Matlab Variables Script Files

Uploaded by

turkisaad65
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

L2 Matlab Variables Script Files

Uploaded by

turkisaad65
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Basic operations in

Matlab

Lecture notes, based on “Introduction to MATLAB


for Engineers”, William Palm, 3rd edition, and other
textbooks & resources

EE 201
9/3/2024 C2 OE 1
Input/output commands
Command Description
disp(A) Displays the contents, but not the name of
the array A.
disp(’text’) Displays the text string enclosed within
quotes.
x = input(’text’) Displays the text in quotes, waits for user
input from the keyboard, and stores the
value in x.
x =input(’text’,’s’) Displays the text in quotes, waits for user
input from the keyboard, and stores the
input as a string in x.
9/3/2024 C2 OE 2
Example

9/3/2024 C2 OE 3
Example

9/3/2024 C2 OE 4
Example:
 Write a statement to accomplish each of the
following:
a) Print the message ‘This is MATLAB program’ on the
MATLAB command window
b) If the variable number is not equal to 7.Print the
message ‘The variable number is not equal to 7’ on
the MATLAB command window
c) Prompt the user to enter the value from keyboard
and store it in the variable x on the MATLAB
command window
d) Print ‘The product is: ’ followed by the value of the
variable result on the MATLAB command window
9/3/2024 C2 OE 5
Solution

9/3/2024 C2 OE 6
Solution

9/3/2024 C2 OE 7
Solution

9/3/2024 C2 OE 8
Solution

9/3/2024 C2 OE 9
Command Description
mod( a, b) Returns the remainder after
division of a by b

Examples:

9/3/2024 C2 OE 10
Script files

 You can perform operations in MATLAB in two


ways:
1. In the interactive mode (similar to using a
calculator), in which all commands are entered
directly in the Command window, or
2. By running a MATLAB program stored in script
file. This type of file contains MATLAB
commands, so running it is equivalent to typing
all the commands - one at a time - at the
Command window prompt.

9/3/2024 C2 OE 11
Script files

 A script files contain a sequence of MATLAB


commands, and is useful when you need to use
many commands or arrays with many elements.

 Script files in MATLAB allows you to write your own


programs and save in M-files, which have the
extension .m,

for example, name of file.m (T_out.m)

9/3/2024 C2 OE 12
Creating M-file
 From the File menu →select New →Script

 Then will see a new edit window called editor window as show
in figure

9/3/2024 C2 OE 13
Keep in mind when using script files:

 The name of a script file MUST begin with a


letter, and may include digits and the
underscore character, up to 31 characters.

 Do not give a script file the same name as a


variable.

 Do not give a script file the same name as a


MATLAB command or function. You can check to
see if a command, function or file name already
exists by using the exist command.
9/3/2024 C2 OE 14
 COMMENTS

The comment symbol (%) may be put anywhere


in the line. MATLAB ignores everything to the
right of the % symbol. For example :

9/3/2024 C2 OE 15
Programming Style

 1.Comments section %
a. The name of the program and any key words in the
first line.
b. The date created, the student‘s names and ID
number in the second line.
c. The definitions of the variable names for every input
and output variable. Include definitions of variables
used in the calculations and units of measurement
for all input and all output variables!
d. The name of every user-defined function
called by the program.

9/3/2024 C2 OE 16
Programming Style

 2. Input section :
Include input data and/or the input functions
and comments for documentation.

 3. Calculation section

 4.Output section
This section might contain functions for
displaying the output on the screen.
9/3/2024 C2 OE 17
Examples of a Script File
 Write a MATALB program that inputs three integers
from the keyboard and prints the sum,average,and
product of these numbers. The screen dialogue
should appear as follows:
The first integer is : 13
The second integer is :27
The third integer is :14
Sum is: 54
Average is:18
Product is :4914
9/3/2024 C2 OE 18
9/3/2024 C2 OE 19
9/3/2024 C2 OE 20
Another way to display the output

disp(['sum is:',num2str(sum)])
disp(['Average is:',num2str(average)])
disp(['Product is:',num2str(product)])

OR
String Formatting
fprintf(‘sum is: %d, Average is:%f, Product is:%d apples\n’, sum, average,
product)
Two main functions exist for formatting text:
1. sprintf: Formats the text and returns it to be stored in a variable
2. fprintf: Formats the text and prints it to the user (Can be also used to print
to a file)
9/3/2024 C2 OE 21
String Formatting

The function takes a format as the first argument to the function, then you add
as many arguments as you included in the format in the same order.

fprintf(‘format’, arg1, arg2…)

Note! Unlike disp, fprintf does not print a new line character after your text.
You have to add it explicitly using the new line character \n .

9/3/2024 C2 OE 22
String Formatting (Format)

The format used for fprintf or sprintf is very powerful and can be as
complicated or as simple as you want it to be. For the purposes of this course,
we will keep it simple.

- There are 3 format specifiers (or placeholders) that you must know

d: for signed whole numbers (135, -120, 0…etc.)

f: for fixed-point notation or numbers with fractions (0.1, 892.123, -123.0


…etc.)

s: for text as string arrays or character arrays (“Hello”, ‘Khalid’…etc.)


9/3/2024 C2 OE 23
Format example

name=‘Omar’
balance=41.23
apples=5

fprintf(‘Hello %s, your balance is %f, you can buy %d apples\n’, name, balance,
apples)

This will output:


Hello Omar, your balance is 41.23, you can buy 5 apples

9/3/2024 C2 OE 24
 You can execute a script file by :

-Go to Debug menu →Run

or

-Press F5

or

-Typing the name of script file without the extension


.m

at the command window


9/3/2024 C2 OE 25
Example of script file
 Write a MATLAB program that
compute the volume of sphere with a
radius 3 cm.

9/3/2024 C2 OE 26
9/3/2024 C2 OE 27
Example of script file
 Write a MATLAB program that
prompts the user to enter a radius
(cm) of sphere to compute the
volume of sphere.

9/3/2024 C2 OE 28
9/3/2024 C2 OE 29

You might also like