10
LAB 3 LAB # 3 SCRIPT AND FUNCTION M-FILE OBJECTIVE To create function M file. To create script M file and calling another function M file. THEORY In Matlab, executable files or exe files have extension .m and hence are called M-files. MATLAB allows you to place MATLAB commands in a simple text file and then tell MATLAB to open the file and evaluate the commands exactly as it would have if you had typed the commands at the MATLAB prompt. These files are called Script files or M-files. The term “script” signifies that MATLAB simply reads from the script found in the file. The term M-file means that script filenames must end with the extension ‘.m’ as in, for example, example1.m Matlab commands are actually built-in Functions. Function m-file contains word function in its first line and the comments in the next line (using % sign) are displayed CE 308: Communication System

LAB 35thsemesternotes.yolasite.com/resources/CS/CS LAB - 03.doc · Web viewLAB # 3 SCRIPT AND FUNCTION M-FILE Objective To create function M file. To create script M file and calling

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: LAB 35thsemesternotes.yolasite.com/resources/CS/CS LAB - 03.doc · Web viewLAB # 3 SCRIPT AND FUNCTION M-FILE Objective To create function M file. To create script M file and calling

LAB 3

LAB # 3

SCRIPT AND FUNCTION M-FILE

OBJECTIVE To create function M file.

To create script M file and calling another function M file.

THEORYIn Matlab, executable files or exe files have extension .m and hence are called M-

files. MATLAB allows you to place MATLAB commands in a simple text file and

then tell MATLAB to open the file and evaluate the commands exactly as it would

have if you had typed the commands at the MATLAB prompt. These files are called

Script files or M-files.

The term “script” signifies that MATLAB simply reads from the script found in the

file. The term M-file means that script filenames must end with the extension ‘.m’ as

in, for example, example1.m

Matlab commands are actually built-in Functions. Function m-file contains word

function in its first line and the comments in the next line (using % sign) are displayed

when help of that function m-file is called. Arguments can be passed to a function file

from another m-file (script).

Function file does not make use of workspace data globally; all the variables

generated, except those passed out, remain local and get destroyed at the end of

execution.

Script m-file makes use of workspace data globally; all the variables generated reside

in workspace and are globally available. Script file can be executed from command

window by just typing its name and pressing enter key.

CE 308: Communication System

Page 2: LAB 35thsemesternotes.yolasite.com/resources/CS/CS LAB - 03.doc · Web viewLAB # 3 SCRIPT AND FUNCTION M-FILE Objective To create function M file. To create script M file and calling

LAB 3

TABLE 3.1 : FUNCTIONS AND THEIR DESCRIPTION

FUNCTION DESCRIPTION

Disp(variable name) Displays results without identifying variable names

Echo Controls Command window echoing of script file contents

as they are executed.

Input Prompts user for input

Keyboard Temporarily gives controls to keyboard. (type return to

return control to the executing script M-file)

Pause or pause(n) Pause until user presses any keyboard key, or pause for n

seconds and then continues

Waitforbuttonpress Pauses until user presses mouse button or any keyboard key.

3.1 CREATING A FUNCTION M-FILE

function y = myavg(z) % computes average of sequence z

L = length(z);

y = (sum(z)/L);

Note: Save the function file – myavg.m

Above program is typed in Matlab editor and is saved as ‘myavg.m’ in folder. If you

type in command window help myavg comment line of this file (computes average of

sequence z) is displayed. This function can also be called by another m-file (script).

3.2 CREATING A SCRIPT M-FILE AND CALLING

FUNCTION M-FILE

% Script m-file mydata.m

p = [1:20];

CE 308: Communication System

Page 3: LAB 35thsemesternotes.yolasite.com/resources/CS/CS LAB - 03.doc · Web viewLAB # 3 SCRIPT AND FUNCTION M-FILE Objective To create function M file. To create script M file and calling

LAB 3

q = [21:40];

disp (‘Average of p is’); myavg(p)

disp (‘Average of q is’); myavg(q) % calculates and displays average of p and q.

Note: Save the function file – mydata.m

Above program is typed in Matlab editor and is saved as ‘mydata.m’ in folder. Now if

you type in command window mydata, it calls the function myavg two times to

calculate average of p and q, and display the result.

CE 308: Communication System

Page 4: LAB 35thsemesternotes.yolasite.com/resources/CS/CS LAB - 03.doc · Web viewLAB # 3 SCRIPT AND FUNCTION M-FILE Objective To create function M file. To create script M file and calling

LAB 3

CLASS ACTIVITY

Show the output of all the given examples:

Example 1% script M-file example1.m

erasers = 4 ; % number of each item

pads = 6;

tape = 2;

Items = erasers + pads+tape

Cost = erasers * 25 + pads * 52 + tape * 99

Avg_cost = Items / Cost

Note: Save the script file – example1.m

Example 2% script M-file example2.m

erasers = 4 ; % number of each item

pads = 6;

tape = input('Enter the number of rolls of tape purchased > ');

Items = erasers + pads + tape

Cost = erasers * 25 + pads * 52 + tape * 99

Avg_cost = Items / Cost

Note: Save the script file – example2.m

Example 3To see the effect of the echo command, add it to the script M-file and execute it

%script M-file example3.m

echo on

erasers = 4 ; % number of each item

pads = 6;

tape = input('Enter the number of rolls of tape purchased > ');

Items = erasers + pads + tape

Cost = erasers * 25 + pads * 52 + tape * 99

Avg_cost = Items / Cost

echo off

Note: Save the script file – example3.mCE 308: Communication System

Page 5: LAB 35thsemesternotes.yolasite.com/resources/CS/CS LAB - 03.doc · Web viewLAB # 3 SCRIPT AND FUNCTION M-FILE Objective To create function M file. To create script M file and calling

LAB 3

Example 4%FINISH Confirm Desire for Quitting MATLAB

question = 'Are you Sure you want to Quit?';

button = questdlg(question,'Exit Request','Yes','No','No');

switch button

case 'No'

quit cancel; % how to cancel quitting!

case 'Yes'

disp('Exiting MATLAB');

save

quit force; % 'Yes' lets script and MATLAB end.

End

Note: Save the script file – finish.m

1. Write function M-file “mysum.m” to add two numbers. The syntax

should be like:

y = mysum(c,d);

Answer:

c=input('Enter the number');d=input('Enter the number'); y=m(c,d)function y = m(z,x) % computes average of sequence z y = z+x;

CE 308: Communication System

Page 6: LAB 35thsemesternotes.yolasite.com/resources/CS/CS LAB - 03.doc · Web viewLAB # 3 SCRIPT AND FUNCTION M-FILE Objective To create function M file. To create script M file and calling

LAB 3

2. Write function M-file “mysqrt.m” and take the sum. The syntax should

be like:

y = mysqrt(c,d);

Answer:

function y = mysqrt(c,d);e=sqrt(c)f=sqrt(d)sum=e+f

CE 308: Communication System

Page 7: LAB 35thsemesternotes.yolasite.com/resources/CS/CS LAB - 03.doc · Web viewLAB # 3 SCRIPT AND FUNCTION M-FILE Objective To create function M file. To create script M file and calling

LAB 3

HOME TASK

3. Write function M-file “cart2plr.m” to convert the Cartesian coordinates

into Polar coordinates. The syntax should be like:

[r,t] = cart2plr(x,y);

4. Write function M-file “quadeq.m” to solve the quadratic equation. The

syntax should be like:

[r1,r2] = quadeq(a,b,c);

function myquadeq(a,b,c);disp('The positive answer of quadratic eq');d=-be=b^2f=4*a*cg=e-fh=sqrt(g)i=2*aj=(d+h)Answer=j/i disp('The negative answer of quadratic eq');l=-bm=b^2n=4*a*co=e-fp=sqrt(g)q=2*ar=(d-h) Answer2=j/i

CE 308: Communication System

Page 8: LAB 35thsemesternotes.yolasite.com/resources/CS/CS LAB - 03.doc · Web viewLAB # 3 SCRIPT AND FUNCTION M-FILE Objective To create function M file. To create script M file and calling

LAB 3

CE 308: Communication System