L2 Matlab Variables Script Files
L2 Matlab Variables Script Files
Matlab
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
9/3/2024 C2 OE 11
Script files
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:
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.
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
name=‘Omar’
balance=41.23
apples=5
fprintf(‘Hello %s, your balance is %f, you can buy %d apples\n’, name, balance,
apples)
9/3/2024 C2 OE 24
You can execute a script file by :
or
-Press F5
or
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