[Excellent] programming specifications for Matlab m Files

Source: Internet
Author: User

Some of them are from: Yang gaobo and others, proficient in MATLAB 7.0 hybrid programming

(1) The function name and file name must be the same;

(2) Naming should reflect their meaning or usage;

From: Lin Rui's high quality c ++/C Programming Guide

The variable name and parameter name should be a combination of "noun" or "Adjective + noun" and a word starting with a lowercase letter.

For example:

Float value;

Float oldvalue;

Float newvalue;

Bool flag;

Int drawmode;

The class name and function name should be a combination of "verb" or "Verb + noun" (verb-bin phrase) and words starting with an upper-case letter.

For example:

Drawbox (); // global function

Class node; // Class Name

Class leafnode; // Class Name

Void draw (void); // function name

Void setvalue (INT value); // function name

Class member functions should only use the "verb". The omitted term is the object itself.

For example:

Box-> draw (); // member functions of the class

Constants all use uppercase letters and underline to separate words.

For example:

Const int max = 100;

Const int max_length = 100;

Static variable prefix S _ (static)

For example:

Static int s_initvalue; // static variable

If you have to use a global variable, add the global variable with the prefix G _ (indicating global ).

For example:

Int g_howmanypeople; // global variable

Int g_howmuchmoney; // global variable

The data member of the class is prefixed with m_( indicating Member). This prevents the data member from having the same name as the parameter of the member function.

For example:

Void object: setvalue (INT width, int height)

{

M_width = width;

M_height = height;

}

To prevent conflicts between some identifiers in a software library and other software libraries, you can add prefixes that reflect the nature of software to various identifiers.

For example:

All library functions of 3D graphics standard OpenGL start with GL, and all constants (or macro definitions) start with GL

Use a correct antgroup to name variables with mutex meanings or functions with opposite actions

For example:

Int minvalue;

Int maxvalue;

Int setvalue (...);

Int getvalue (...);

Avoid the number in the name unless the number is required logically.

For example:

Value1, value2, etc.

Do not show similar identifiers that are case sensitive.

For example:

Int X, X; // variable X and X are easy to confuse

Void Foo (int x); // The Foo and foo functions are easy to confuse.

Void Foo (float X );

Do not show local variables and global variables with identical identifiers


(3) Some abbreviated naming rules;

It must start with 3-4 letters.

Example: EXP = expert expe = experience Lux = luxury prosperous = pros

First and last letter

Example: yr = year Tx = telex

Remove vowels

Example: pls = Please RCV = receive RGD = registered DMST = demonstration

Homophonic Method

For example, u = You 8 = ate 4 = for 2 =

Symbol Method

Example:> = more than <= less than P + 3% = price increase 3%

(4) MATLAB supports mutual calls between functions;

The M file can contain more than two functions. The first function is the main function, which can be called by the M file name. Except the main function, the other functions are subfunctions. The sub-function can only be called by the main function and sub-function in this file. The primary function must be at the top, and the subfunctions can be arranged in any order.

(5) The function can be called based on the input and output variables less than the ones specified in the function M file, but not more than the number of input and output variables specified in the function M file;

The variable nargin contains the number of input parameters, and the variable nargout contains the number of output parameters.

(6) functions have their own dedicated workspaces, which are separated from those of Matlab;

(7) In actual programming, global variables should be avoided whenever possible;

Note: The global variable can have only one value, but cannot be a vector or a matrix. The parameters between functions can pass a vector.

(8) If you enter a symbol in MATLAB, search by variable, built-in function, and external function;

(9) The script file can be called from the function M file;

In this case, you can view the function workspace in the script file instead of the MATLAB workspace. The script file called from the function M file does not need to call the function to compile to the memory. Each time a function is called, it is opened and interpreted. Therefore, calling the script file from the function M file slows down the execution of the function.

(10) functions can be called recursively, that is, M file functions can call them themselves;

Make sure it is terminated, otherwise MATLAB will be stuck in an endless loop.

(11) when the function M file reaches the terminal of the M file, or the return command return is returned, the execution and return are completed.

(12) The Matlab function error () displays a string in the Command window, giving up function execution, and returning control to the keyboard.

This function is useful for prompting function improper use.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.