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
通过上述内嵌函数,我们可以看到函数设置的五个要素:
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
首先新建脚本,输入函数,保存为与函数名相同名字的文件。
First create a new script, enter the function, and save it as a file with the same name as the function.
注意:元素与元素之间是点乘。
然后就可以调用该函数了。
Note: There is a dot product between elements.
Then you can call the function.
调用内嵌函数和自定义函数是一致的。例如可以计算两组数据。
Calling built-in functions is consistent with custom functions. For example, two sets of data can be calculated.
Part 4
多输入、多输出函数
如定义下列函数
Such as defining the following function
然后调用函数Then call the function
我们会发现结果不对,多输入多输出函数的正确使用方法如下:
We will find that the result is wrong. The correct way to use the multiple-input multiple-output function is as follows:
Part 5
没有input函数
例如,在命令栏输入一个华氏温度,然后计算成摄氏温度。
For example, enter a Fahrenheit temperature in the command bar, and then calculate it to Celsius.
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: