Chapter 3-1 Programmimg Style
Chapter 3-1 Programmimg Style
Matlab
EE 201
C3-1 2022 1
Script files
C3-1 2022 2
Script files
C3-1 2022 3
Creating M-file
From the File menu →select New →Script
Then will see a new edit window called editor window as show
in figure
C3-1 2022 4
Keep in mind when using script files:
C3-1 2022 6
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.
C3-1 2022 7
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.
C3-1 2022 8
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 : 10
The second integer is :20
The third integer is :30
Sum is: 60
Average is:20
Product is :6000
C3-1 2022 9
% Find the Sum,Average and Product of any three
numbers
% hint: 1) Get three Number as Input 2) Calculate
Sum,Average and Product
% using formulae 3) Display the results
% Input Section
x=input(‘The first integer is : ');
y=input(‘The Second integer is : ');
z=input(' The third integer is : ');
% Calculation Section
sum=x+y+z;
average= sum/3;
product= x*y*z;
% Display Section
disp(['sum is : ',num2str(sum)])
disp(['Average is : ',num2str(average)])
disp(['Product is : ',num2str(product)])
C3-1 2022 10
% Find the Sum,Average and Product of any three numbers
% hint: 1) Get three Number as Input 2) Calculate Sum,Average
and Product
% using formulae 3) Display the results
% Input Section
x=input('enter the value of x= ');
y=input('enter the value of y= ');
z=input('enter the value of z= ');
% Calculation Section
sum=x+y+z;
average= sum/3;
product= x*y*z;
% Display Section
disp('sum of three numbers=‘),disp(sum)
disp('average of three numbers=‘),disp(average)
disp('product of three numbers=‘),disp(product)
C3-1 2022 12
Answer
>> C3_1_example1
C3-1 2022 13
You can execute a script file by :
or
C3-1 2022 14
Example of script file
Write a MATLAB program that
compute the volume of sphere with a
radius 3 cm.
C3-1 2022 15
C3-1 2022 16
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.
C3-1 2022 17
C3-1 2022 18
HW
Write a MATLAB program that prompts the
user to enter the radius of a circle and
calculate its area and its circumference
𝑆𝐴 = 4𝜋 2 𝑅𝑟 V=2𝜋 2 𝑅𝑟 2
C3-1 2022 20