Theme Graphic
Theme Graphic

Learning by examples

tutorial,example,tips,maple,matlab,C++,visual C++,java,C#,php,mysql,database,programming,computer

Subscribe

Author

Archive

Tags

Posted on Monday, May 24, 2010 at 2:39 PM

Matlab Tutorial 5 - M-files: Scripts and functions


Matlab tutorial

<p>To take advantage of MATLAB's full capabilities, we need to know how to construct long (and sometimes complex) sequences of statements. This can be done by writing the commands in a file and calling it from within MATLAB. Such files are called "m-files" because they must have the filename extension "<code> .m</code>". This extension is required in order for these files to be interpreted by MATLAB.<!--more--></p> <p> There are two types of m-files: <code>script files</code> and <code>function files</code>. <strong>Script files</strong> contain a sequence of usual MATLAB commands, that are executed (in order) once the script is called within MATLAB. For example, if such a file has the name <code>compute.m</code> , then typing the command <code>compute </code>at the MATLAB prompt will cause the statements in that file to be executed. Script files can be very useful when entering data into a matrix. </p> <p> <strong>Function files</strong>, on the other hand, play the role of user defined commands that often have input and output. You can create your own commands for specific problems this way, which will have the same status as other MATLAB commands. Let us give a simple example. The text below is saved in a file called <code>log3.m</code> and it is used to calculate the base 3 logarithm of a positive number. The text file can be created in a variety of ways, for example using the built-in MATLAB editor through the command <code>edit</code> (that is available with MATLAB 5.0 and above), or your favorite (external) text editor (e.g. <code>Notepad</code> or <code>Wordpad</code> in Microsoft Windows). You must make sure that the filename has the extension " <code>.m</code>" ! </p> <pre name="code" class="js"> function [a] = log3(x) % [a] = log3(x) - Calculates the base 3 logarithm of x. a=log(abs(x))./log(3); % End of function end </pre> Using this function within MATLAB to compute log<sub>3</sub>(5), we get <pre name="code" class="js"> >> log3(5)

ans =

1.4650 </pre> Let us explain a few things related to the syntax of a function file. Every MATLAB function begins with a header, which consists of the following : (a) the word <code>function</code>, (b) the output(s) in brackets, (the variable a in the above example) (c) the equal sign, (d) the name of the function, which must match the function filename (<code>log3 </code>in the above example) and (e) the input(s) (the variable <code>x</code> in the above example).

Any statement that appears after a "<code>%</code> " sign on a line is ignored by MATLAB and plays the role of comments in the subroutine. Comments are essential when writing long functions or programs, for clarity. In addition, the first set of comments after the header in a function serve as on-line help. For example, see what happens when we type <pre name="code" class="js"> >> help log3 [a] = log3(x) - Calculates the base 3 logarithm of x. </pre> MATLAB gave us as "help" on the function we defined, the text that we included after the header in the file.

Finally, the algorithm used to calculate the base 3 logarithm of a given number, is based on the formula <code> log<sub>3</sub>(x)=ln(|x|)/ln(3). </code> Since the logarithm of a negative number is undefined, we use the absolute value for "safety". Also, note that we have allowed for a vector to be passed as input, by using element-wise division in the formula.

During a MATLAB session, we may call a function just like we did in the above example, provided the file is saved in the current (working) directory. This is the reason why in the beginning of this guide we suggested that you should create a working directory and switch to that directory from within MATLAB.

It should be noted that both types of m-files can reference other m-files, including themselves in a recursive way.

<strong>Catch up</strong> <a href="http://learnbyexamples.org/matlab/matlab-tutorial-1-the-basic-features.html">Matlab Tutorial 1 – The basic features</a> <a href="http://learnbyexamples.org/matlab/matlab-tutorial-2-vectors-and-matrices.html">Matlab Tutorial 2 – Vectors and matrices</a> <a href="http://learnbyexamples.org/matlab/matlab-tutorial-3-built-in-functions.html">Matlab Tutorial 3 – Built-in functions</a> <a href="http://learnbyexamples.org/matlab/matlab-tutorial-4-plotting.html">Matlab Tutorial 4 – Plotting</a>

Matlab tutorial
Bookmark: Submit To Digg Submit To reddit Submit To del.icio.us Bookmark With StumbleUpon Bookmark With FaceBook Bookmark With Google Bookmarks   Share: Share By Email By Email

0 comments on "Matlab Tutorial 5 - M-files: Scripts and functions"
No comments posted yet.

Leave A Comment
Subject:


Comment:
   Bold Italic Underline          Code Link Image Horizontal Rule


Because you do not have or are not logged in to your Programmer's Heaven account, please enter your name.

Name:


To help prevent comment SPAM, please enter the magic code '214' in the box:




Posting Rules
Please follow these rules when posting comments on blog posts.
  • Do not post anything that is racist, hate speech or of a sexual or adult nature.
  • Do not post or link to anything that infringes copyrighted laws.
  • Posting about security or legal topics is fine so long as you are not glorifying or encouraging people to perform illegal activities.
  • Both the author of this blog and the Programmer's Heaven administrators may delete any inappropriate comments without notice at their own discretion.
 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.