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

Chapter 3-1 Programmimg Style

This document provides an overview of using script files in MATLAB. It discusses two ways to perform operations: interactively in the command window or by running a script file containing MATLAB commands. Script files allow storing and running multiple commands at once. They are created and saved with the .m extension. The document provides examples of script files that input values, perform calculations, and output results. It also discusses commenting in script files and guidelines for programming style.

Uploaded by

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

Chapter 3-1 Programmimg Style

This document provides an overview of using script files in MATLAB. It discusses two ways to perform operations: interactively in the command window or by running a script file containing MATLAB commands. Script files allow storing and running multiple commands at once. They are created and saved with the .m extension. The document provides examples of script files that input values, perform calculations, and output results. It also discusses commenting in script files and guidelines for programming style.

Uploaded by

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

Basic operations in

Matlab

EE 201
C3-1 2022 1
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.

C3-1 2022 2
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

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:

 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.
C3-1 2022 5
 COMMENTS

The comment symbol (%) may be put anywhere


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

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)])

If you enter as sum

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)

disp(['sum of three numbers = ',num2str(sum)])


disp(['Average of three numbers = ',num2str(average)])
disp(['Product of three numbers = ',num2str(product)])

If you enter as sum C3-1 2022 11


fprintf('sum of three numbers =%d\n',sum)
fprintf('Average of three numbers =%d\n',average)
fprintf('Product of three numbers =%d\n',product)

C3-1 2022 12
Answer
>> C3_1_example1

enter the value of x= 10

enter the value of y= 20

enter the value of z= 30

sum of three numbers = 60

Average of three numbers = 20

Product of three numbers = 6000

C3-1 2022 13
 You can execute a script file by :

-Go to Debug menu →Run

or

-Typing the name of script file without the extension


.m

at the command window

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

The area of a circle is given by the formula


A = π r2, where A is the area and r is the
radius.

The circumference of a circle is C = 2 π r.


C3-1 2022 19
HW
 Write a MATLAB program that
prompts the user to enter the
inner radius(r) and the outer
radius (R) of a toroid and
calculate its Surface area (SA)
and Volume (V).

 𝑆𝐴 = 4𝜋 2 𝑅𝑟 V=2𝜋 2 𝑅𝑟 2
C3-1 2022 20

You might also like