Skip to content Skip to navigation

OpenStax_CNX

You are here: Home » Content » An Introduction to MATLAB: Editing Files and Creating Functions (PC)

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Endorsed by Endorsed (What does "Endorsed by" mean?)

This content has been endorsed by the organizations listed. Click each link for a list of all content endorsed by the organization.
  • IEEE-SPS

    This module is included inLens: IEEE Signal Processing Society Lens
    By: IEEE Signal Processing SocietyAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Click the "IEEE-SPS" link to see all content they endorse.

  • College Open Textbooks display tagshide tags

    This module is included inLens: Community College Open Textbook Collaborative
    By: CC Open Textbook CollaborativeAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Comments:

    "Reviewer's Comments: 'I recommend this book as a "required primary textbook." This text attempts to lower the barriers for students that take courses such as Principles of Electrical Engineering, […]"

    Click the "College Open Textbooks" link to see all content they endorse.

    Click the tag icon tag icon to display tags associated with this content.

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • Bookshare

    This module is included inLens: Bookshare's Lens
    By: Bookshare - A Benetech InitiativeAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Comments:

    "Accessible versions of this collection are available at Bookshare. DAISY and BRF provided."

    Click the "Bookshare" link to see all content affiliated with them.

  • NSF Partnership display tagshide tags

    This module is included inLens: NSF Partnership in Signal Processing
    By: Sidney BurrusAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Click the "NSF Partnership" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Featured Content display tagshide tags

    This module is included inLens: Connexions Featured Content
    By: ConnexionsAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Comments:

    "A First Course in Electrical and Computer Engineering provides readers with a comprehensive, introductory look at the world of electrical engineering. It was originally written by Louis Scharf […]"

    Click the "Featured Content" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Also in these lenses

  • UniqU content

    This module is included inLens: UniqU's lens
    By: UniqU, LLCAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Click the "UniqU content" link to see all content selected in this lens.

  • Lens for Engineering

    This module is included inLens: Lens for Engineering
    By: Sidney Burrus

    Click the "Lens for Engineering" link to see all content selected in this lens.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

An Introduction to MATLAB: Editing Files and Creating Functions (PC)

Module by: Louis Scharf. E-mail the author

Note:

This module is part of the collection, A First Course in Electrical and Computer Engineering. The LaTeX source files for this collection were created using an optical character recognition technology, and because of this process there may be more errors than usual. Please contact us if you discover any errors.

You should write a MATLAB program whenever you anticipate exe- cuting some sequence of statements several times or again in a later session. On an IBM PC, you may use any text editor to write a program, as long as the file can be saved in ASCII format without the control codes used by most word processors. Applicable text editors include Edix, Wordstar, XTree Pro, and Turbo Pascal's editor.

Editing Files. If you have enough memory, you can run your editor without leaving MATLAB by using the exclamation point (!), like this:

≫ !EDIX

The exclamation point may be used to execute any DOS command or program from MATLAB. When the command or program finishes, your MATLAB variables are just as you left them. Use your editor to write program lines just as you would type them in MATLAB's command mode. Then save the file with extension .m in the directory where you will run MATLAB. Such MATLAB programs are called m-files. You may run your m-file by typing the file name (without the .m extension) at the MATLAB command prompt ()().

Script Files. There are two kinds of m-files, called script files and functions. Running a script file is exactly like typing the commands it contains at the prompt. Your m-file will automatically be a script file unless you specify otherwise, as described later. Practice by entering, saving, and running plotsin.m as listed next:

t = -6:.2:6;
y = sin(t);
plot(t,y)
title('SINE')
pause
grid
xlabel('t')
ylabel('sin(t)')

When the pause is executed, you will need to press a key to go on. If you type whos after running plotsin, you will see that the variables t and y remain in memory. Comments are important to a script file. They are marked with the symbol %. Anything following this symbol on a line is assumed to be a comment and is ignored by the MATLAB program interpreter.

Functions. Functions differ from script files in that they have designated input and output variables. Any other variables used within a function are local variables, which do not remain after the function terminates and which have no effect on variables outside the function. Many of the functions supplied with MATLAB are actually m-files. A good example is triu.m:

≫ type triu

The word function at the beginning of the file makes it a function rather than a script file. The function name in this line must match the file name. The input variables of triu are x and k, meaning that the first input argument will be referred to as x and the second as k within the function. Likewise, the function line designates y as the output. There is nothing special about the variable names x,k, and y when the function is used. It is only that whatever inputs and output you use will be referred to as x,k, and y inside the function. The variables m,n,j, and i are created temporarily when triu runs and disappear when it terminates. They are local variables and have no effect on variables with the same names outside the function. In contrast, a script file has no local variables and does no substitution of input and output variable names.

As an exercise, enter and save the function perp.m:

      function y=perp(x)
      % PERP(x) is a complex number perpendicular to x.
      j = sqrt(-1);
      y = j*real(x)-imag(x);

Evaluate perp on various complex numbers. Replace the last line by y = x*j;. Do you get the same result? Why?

Printing Files and Graphics. To display an m-file on the screen, use the instruction ≫ type filename. To make a copy at a printer, use the DOS command ≫ !print filename.m. Graphics hardcopy is available through the commands meta and gpp. See the MATLAB manual for more information.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks