Simple way to define functions in Matlab

1. m file definition function (m-function)

  1. Basic structure of Matlab function
function[输出形参表: output1, ...,outptn] = 函数名(输入形参表: input1, ... , inputn)

注释说明部分
函数体代码部分

end

Description: function: Indicates that a function is written;

The output formal parameter list, that is, the return value of the function, each return value is separated by a comma; (it does not need to be assigned by return to the return value like in C language, and the corresponding parameters of outp_args can be used directly)

Function name: need to be consistent with the m file name

Input parameter list: the input parameters of the function, separated by commas;

Function body: complete the specific function of the function, which is written according to specific needs; it is realized by using "function input parameters", "custom variables", and "flow control structure";

end: Indicates the end of this function, which can also be omitted.

  1. function call

The defined function is saved as an m-file with the same function name, and placed under the current path of Matlab (or modify the current path to the path where the m-function file is located), and then use the function that comes with Matlab directly in the command window Or use the function in the program code is to call it. When calling a function, you only need to know what the function does, as well as its input parameters and output parameters.

The general format of a function call is:

[输出实参表]=函数名(输入实参表)

Note 1: When a return statement is encountered in the function, the function body will be exited, and the function call ends;

Note 2: One or several functions can also be defined in the function body, called sub-functions; Note: sub-functions can only exist in the body of the main function, not independently; the position of the sub-functions in the body of the main function can be arbitrary, without affecting the use ;The sub-function can only be called by the main function and other sub-functions under the same main function body, but the sub-function "handle" is an exception;

Note 3: When calling a function, Matlab uses two permanent variables nargin and nargout to record the number of input actual parameters and output actual parameters when calling the function respectively. As long as these two variables are included in the function file, the number of input and output parameters when the function file is called can be accurately known, so as to determine how the function is processed.

Example 1 Function (sub-function) definition and call example, find the maximum and minimum value of the vector.

编写m-函数文件:max_min_values.m


function [max,min] = max_min_values(X)

%输入参数X为数值向量,返回其最大值和最小值

max=mysubfun1(X);

min=mysubfun2(X);

function r=mysubfun1(X) %子函数1

x1=sort(X, 'descend');

r=x1(1);

end

function r=mysubfun2(X)  %子函数2

x1=sort(X);

r=x1(1);

end

end
调用函数(命令窗口):

A = [34,56,23,11,2,39];

[m,n]=max_min_values(A)

运行结果: m = 56
          n = 2

1. Function file + call command file: need to define a custom function M file separately

Example: Custom sum function

Need to customize a my_sum.m file to store the function

function[s]=my_sum(n)
s=0;
for 
    i=1:n
    s=s+i;
end

Note: The function name (my_sum) must be consistent with the file name (my_sum.m).
When calling the function, the function file is placed under the current working directory.

2. Function file + sub-function: define an M-file with multiple sub-functions

Example: find the maximum function

function son_function( )        %主函数必须位于最上方
%子函数举例
max1=find_max(1,2,3)
max2=find_max(7,3,9)

function max=find_max(a,b,c)    %子函数
if (a>=b)&(a>=c)
    max=a;
elseif (b>=a)&(b>=c)
    max=b;
else
    max=c;
end

Note: Multiple functions can be written in one m file, but only the first one can be called in other files, and the remaining functions can only be called in this file.

The function name should be the same as the main function name

Guess you like

Origin blog.csdn.net/msmsa/article/details/129859205