MATLAB PROGRAMMING TIPS SHAMEER A KOYA MATLAB Scripts
Script  MATLAB Script or programs are sequences of MATLAB commands saved in plain text files.  MATLAB requires that these files have the extension ‘.m’.  When you type the name of the script file at the MATLAB prompt the commands in the script file are executed as if you had typed them in command window.  Why use Scripts?  Use same code in more than one place in program without rewriting code  Reuse code by calling in different programs  Code for a script is done in an Editor window and saved as m-file. 2
Typing a Program  If you are using the command prompt, type edit in the command prompt.  This will open the editor.  You can directly type edit and then the filename (with .m extension)  Alternatively, if you are using the IDE, choose NEW -> Script.  This also opens the editor and creates a file named Untitled.  You can name and save the file after typing the code. 3
Opening A Script file 4
Sample Script 5
Run the Script  After creating and saving the file, you can run it in two ways:  Clicking the Run button on the editor window  Just typing the filename in the command prompt.  When typing the filename in the command window do not include ".m".  In case your code has errors, MATLAB will show an error message in the command window, when you try to run the program .  Error message will be hyperlinked to the line in the file that caused the error.  Interpret the error message and make necessary changes to you code in the editor. 6
Run the Script … 7
Special Note  Apart from syntax errors, perhaps the most common problem with using script files is that MATLAB cannot locate the script file you wish to execute.  MATLAB gives the error message ??? Undefined function or variable ...  It is not immediately clear why the function is undefined when its m-file is clearly visible to the user.  MATLAB cannot execute a function unless it knows where to find its m-file.  This requires that the function be in the internal MATLAB path. 8
Script variables are Global !!  Script variables are added to the workspace.  If you use the same variables in another script, they will be modified by that script.  This may result in a confusing mix of variables in the MATLAB workspace.  Some times it may lead to a syntax error or may lead to subtle errors in the result. 9

Introduction to Matlab Scripts

  • 1.
    MATLAB PROGRAMMING TIPS SHAMEERA KOYA MATLAB Scripts
  • 2.
    Script  MATLAB Scriptor programs are sequences of MATLAB commands saved in plain text files.  MATLAB requires that these files have the extension ‘.m’.  When you type the name of the script file at the MATLAB prompt the commands in the script file are executed as if you had typed them in command window.  Why use Scripts?  Use same code in more than one place in program without rewriting code  Reuse code by calling in different programs  Code for a script is done in an Editor window and saved as m-file. 2
  • 3.
    Typing a Program If you are using the command prompt, type edit in the command prompt.  This will open the editor.  You can directly type edit and then the filename (with .m extension)  Alternatively, if you are using the IDE, choose NEW -> Script.  This also opens the editor and creates a file named Untitled.  You can name and save the file after typing the code. 3
  • 4.
  • 5.
  • 6.
    Run the Script After creating and saving the file, you can run it in two ways:  Clicking the Run button on the editor window  Just typing the filename in the command prompt.  When typing the filename in the command window do not include ".m".  In case your code has errors, MATLAB will show an error message in the command window, when you try to run the program .  Error message will be hyperlinked to the line in the file that caused the error.  Interpret the error message and make necessary changes to you code in the editor. 6
  • 7.
  • 8.
    Special Note  Apartfrom syntax errors, perhaps the most common problem with using script files is that MATLAB cannot locate the script file you wish to execute.  MATLAB gives the error message ??? Undefined function or variable ...  It is not immediately clear why the function is undefined when its m-file is clearly visible to the user.  MATLAB cannot execute a function unless it knows where to find its m-file.  This requires that the function be in the internal MATLAB path. 8
  • 9.
    Script variables areGlobal !!  Script variables are added to the workspace.  If you use the same variables in another script, they will be modified by that script.  This may result in a confusing mix of variables in the MATLAB workspace.  Some times it may lead to a syntax error or may lead to subtle errors in the result. 9