Part 1

Matlab函数的基本概念

1.MATLAB的脚本和函数都是包含MATLAB命令的.m文件。

2.脚本没有参数的输入输出,函数有。

3.使用函数,需要一个脚本或者函数召唤它。

1. MATLAB scripts and functions are .m files containing MATLAB commands.

2. The script has no input and output parameters, but the function has.

3. To use a function, you need a script or function to call it.

Part 2

查看内嵌函数

如查看平均数函数

Such as viewing the average function

matlab调用函数文件(matlab函数调用详细步骤)(1)

matlab调用函数文件(matlab函数调用详细步骤)(2)

通过上述内嵌函数,我们可以看到函数设置的五个要素:

Through the above embedded function, we can see the five elements of the function setting:

1.函数使用的关键字是function

2.文件名与函数名字是一致的

3.(x,dim,flag)是输入,y是输出

4.dim和flag为局部变量

5.MATLAB需要有找到该函数的路径

1. The keyword used by the function is function

2. The file name is consistent with the function name

3. (x, dim, flag) is input, y is output

4.dim and flag are local variables

5. MATLAB needs a path to find the function

Part 3

用户自定义函数

如自定义自由落体位移函数

Such as custom free fall displacement function

matlab调用函数文件(matlab函数调用详细步骤)(3)

首先新建脚本,输入函数,保存为与函数名相同名字的文件。

First create a new script, enter the function, and save it as a file with the same name as the function.

matlab调用函数文件(matlab函数调用详细步骤)(4)

注意:元素与元素之间是点乘。

然后就可以调用该函数了。

Note: There is a dot product between elements.

Then you can call the function.

matlab调用函数文件(matlab函数调用详细步骤)(5)

调用内嵌函数和自定义函数是一致的。例如可以计算两组数据。

Calling built-in functions is consistent with custom functions. For example, two sets of data can be calculated.

matlab调用函数文件(matlab函数调用详细步骤)(6)

Part 4

多输入、多输出函数

如定义下列函数

Such as defining the following function

matlab调用函数文件(matlab函数调用详细步骤)(7)

matlab调用函数文件(matlab函数调用详细步骤)(8)

然后调用函数Then call the function

matlab调用函数文件(matlab函数调用详细步骤)(9)

我们会发现结果不对,多输入多输出函数的正确使用方法如下:

We will find that the result is wrong. The correct way to use the multiple-input multiple-output function is as follows:

matlab调用函数文件(matlab函数调用详细步骤)(10)

Part 5

没有input函数

例如,在命令栏输入一个华氏温度,然后计算成摄氏温度。

For example, enter a Fahrenheit temperature in the command bar, and then calculate it to Celsius.

matlab调用函数文件(matlab函数调用详细步骤)(11)

Isempty:判断该函数是否为空

Num2str:将数值转化为字符串

然后运行程序,不停的输入,结果如下:

Isempty: Determine whether the function is empty

Num2str: Convert a value to a string

Then run the program, keep typing, the results are as follows:

matlab调用函数文件(matlab函数调用详细步骤)(12)