Skip to content Skip to navigation Skip to collection information

OpenStax_CNX

You are here: Home » Content » A First Course in Electrical and Computer Engineering » An Introduction to MATLAB: Editing Files and Creating Functions (Macintosh)

Navigation

Table of Contents

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 collection is included inLens: IEEE Signal Processing Society Lens
    By: IEEE Signal Processing Society

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

  • College Open Textbooks display tagshide tags

    This collection is included inLens: Community College Open Textbook Collaborative
    By: CC Open Textbook Collaborative

    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 collection is included inLens: Bookshare's Lens
    By: Bookshare - A Benetech Initiative

    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 collection is included inLens: NSF Partnership in Signal Processing
    By: Sidney Burrus

    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 collection is included inLens: Connexions Featured Content
    By: Connexions

    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 collection is included inLens: UniqU's lens
    By: UniqU, LLC

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

  • Evowl

    This collection is included inLens: Rice LMS's Lens
    By: Rice LMS

    Comments:

    "Language: en"

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

  • Lens for Engineering

    This module and collection are 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 (Macintosh)

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.

If you quit MATLAB now, all the commands you have typed will be lost. This is where the Edit window is useful. If you choose new from the File menu, a new window appears with the title Edit 1: Untitled. In this window, you should type in all the commands you would like MATLAB to execute at once. When you are finished typing, you may save the file by choosing save or save as in the file menu and by entering a name for the file. If the edit window is active (that is, if it appears in front), then choose save and go from the file menu to save the file and execute it. If the command window is active, then you can execute the file by entering its name.

Editing Files. To test your understanding of file editing, enter the following commands in a file named myfile:


clear, clg                      %Clear variables and graphics
j=sqrt(-1);                     %To be sure
z1=1+.5*j,z2=2+1.5*j            %Enter variables
z3=z1+z2,z4=z1*z2               %Compute sum and product
axis([0 4 0 4]),                %First plot
  axis('square'),plot(z1,'o')
hold on                         %Allow overplot
plot(z2,'o'),plot(z3,'+'),      %Other plots
    plot(z4,'*')
hold off

You do not have to type the % sign and the text that follows it. These are simply comments in a file. They are ignored by the MATLAB interpreter. You should, however, make a habit of adding comments (preceded by %) to your file if you want to be able to understand programs that have been written long ago.

Do not forget to save your file. Such a file is called a script file. It contains MATLAB commands that could have been entered one by one in the command window. You have three ways to execute a script file:

  1. with the edit window active, choose save and go from the file menu;
  2. with the command window active, enter the file name; or
  3. with the command window active, choose run script... from the M-file menu. In this case, a menu pops up to ask you which file you want to execute.

Try each of these three methods in order to get used to their differences. Figure 1 shows the plot that you should get.

Creating Functions. MATLAB puts many commands at your disposal, and you just have to enter their names (with or without arguments) to execute them. Some commands are built in to MATLAB, and others are contained in files to which you have access (not to modify them, but to see how things are done). You can try to display the contents of a command file in the command window by entering the command type filename. Enter type plot. You should get the message

??? Built-in function.
Figure 1: A Typical Graph (©Apple Computer, Inc., used with permission.)
This is an image of an old apple computer screen, with some matlab code in a window in the background and a graph in a window in the foreground.

This means that the command plot is a build-in function and thus cannot be typed by the user (it is compiled with the program). Similarly, try to type the function file containing the command sinh by entering the command type sinh. You should get


function y=sinh(x)
%SINH SINH(X) is the hyperbolic sine of the elements of X.
y=(exp(x)-exp(-x))/2;

This is a typical example of a function file. It differs from a script file by the fact that the first line contains the word function. What it does is make the command y = sinh(x) equivalent to the command y=(exp(x)-exp(-x))/2 . The variable x is the input argument (there could be more than one), and y is the output argument (there also could be more than one). The second line contains comments about the function and its arguments. They can spread to several lines. Display them by typing the command help sinh. The third line contains the actual function commands (there could also be several lines).

As an exercise, enter the function perp(x):


function y=perp(x)
%PERP PERP(X) is a complex value 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?

Note that local variables are just that–local. Only the input and out- put arguments are kept in memory after the function is called and executed. For more on functions and multiple arguments, see the MATLAB manual. In the previous example, the variable jj is local. If, before using the function perp, you use the same variable j j, its contents will not be affected by the command perp(z). Verify this.

Normally, while a file (script or function) is executed, the commands are not displayed on the command window. Just the results are displayed. The command echo allows you to view all the instructions. This is useful for debugging and demonstrations. It is turned off by typing echo off.

Collection Navigation

Content actions

Download:

Collection 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 ...

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:

Collection 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

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