متلب
صفحات وبلاگ
نویسنده: پ متلب - جمعه ۱۸ فروردین ،۱۳٩۱

MATLAB Function (*.m files) Editing, Executing and Displaying Help


MATLAB functions are just script files and must have the MATLAB extension "m", such as "circle.m".

  • Editing MATLAB Function Files: Modifying or creating or saving MATLAB script function files, whether on PCs/Macs or Unix/Linux workstations, are edited the same way you normally edit files on those platforms:
    1. PCs/Macs: To create a new function run you favorite Edit application, select a "New" file, type in your MATLAB commands for the function, and "Save As" file name [function].m when finished. If the function script file already exists, that start with "OPEN File" instead.
    2. Unix/Linux workstations: To create a new script function or to modify an existing one type in the command of your favorite editor like vi, emacs or other with the name [function].m as argument, type in your MATLAB commands for the function, and save when finished. Editing can also be done on the Unix/Linux MATLAB command line using the bang escape "!", for example

       

      >> !vi [function].m 
      but "!emacs" can also be used, yet beware of carpel tunnel syndrome.

    Hint: Square brackets are used here to denote general items for the user as to type in actual names when actually used in MATLAB, such as "[function]", but in MATLAB square brackets are also used for marking arrays that are being initialized on assignment.

    Hints : The percent sign "%" marks the beginning of a comment, so if a "%" appears in a line (except as a format specifier in a "fprintf" or "sprintf" argument, its other purpose), the rest of the line to the right is a comment, but the first set of lines marked at the beginning of the line before any MATLAB commands are Header lines that are listed when you use the MATLAB "help" command:

     

    >> help [function] 
    for the script file [function].m, whether user or MATLAB built-in function. Also, the header lines are also search for keywords by the MATLAB "lookfor" command, e.g.,

     

    >> lookfor [keyword] 

    Hints: The semicolon ";" at the end of a MATLAB command line suppressed MATLAB output for that command, but semicolon are also used for separating rows in array initialization forms.

  • Executing an Existing Function Files: In MATLAB, type the name of the function without the ".m" extension on the command line, for example for circle.m, type

     

    >> circle 
  • Displaying MATLAB Function Script File Content In MATLAB: On the MATLAB command line use the "type" command with the ".m" extension, for example,

     

    >> type circle.m 
  • Sample MATLAB Function Script File: The simple example "circle.m should plot a unit circle when executed:

     

    % Circle - Script file to draw unit circle % modified from "Getting Started with MATLAB" by Rudra Pratap 9/14/94 format compact % tightens loose format format long e % makes numerical output in double precision theta = linspace(0,2*pi,100); % create vector theta x = cos(theta); % generate x-coordinate y = sin(theta); % generate y-coordinate plot(x,y); % plot circle axis('equal'); % set equal scale on axes per pixel title('Circle of unit radius') % put title c=2*pi % prints out 2*pi value disp('end circle.m') % prints out literal string. % End Circle
    

Web Source: http://www.math.uic.edu/~hanson/MATLAB/MATLABfunctions.html

Email Comments or Questions to hanson@uic.edu

نویسندگان وبلاگ:
کدهای اضافی کاربر :