matlab调用函数文件matlab函数调用详细步骤matlab调用函数文件




matlab调用函数文件matlab函数调用详细步骤matlab调用函数文件

2022-07-21 2:24:53 网络知识 官方管理员


Part1

Matlab函数的基本概念

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

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

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

1.MATLABscriptsandfunctionsare.mfilescontainingMATLABcommands.

2.Thescripthasnoinputandoutputparameters,butthefunctionhas.

3.Touseafunction,youneedascriptorfunctiontocallit.

Part2

查看内嵌函数

如查看平均数函数

Suchasviewingtheaveragefunction

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

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

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

Throughtheaboveembeddedfunction,wecanseethefiveelementsofthefunctionsetting:

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

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

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

4.dim和flag为局部变量

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

1.Thekeywordusedbythefunctionisfunction

2.Thefilenameisconsistentwiththefunctionname

3.(x,dim,flag)isinput,yisoutput

4.dimandflagarelocalvariables

5.MATLABneedsapathtofindthefunction

Part3

用户自定义函数

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

Suchascustomfreefalldisplacementfunction

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

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

Firstcreateanewscript,enterthefunction,andsaveitasafilewiththesamenameasthefunction.

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

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

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

Note:Thereisadotproductbetweenelements.

Thenyoucancallthefunction.

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

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

Callingbuilt-infunctionsisconsistentwithcustomfunctions.Forexample,twosetsofdatacanbecalculated.

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

Part4

多输入、多输出函数

如定义下列函数

Suchasdefiningthefollowingfunction

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

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

然后调用函数Thencallthefunction

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

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

Wewillfindthattheresultiswrong.Thecorrectwaytousethemultiple-inputmultiple-outputfunctionisasfollows:

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

Part5

没有input函数

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

Forexample,enteraFahrenheittemperatureinthecommandbar,andthencalculateittoCelsius.

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

Isempty:判断该函数是否为空

Num2str:将数值转化为字符串

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

Isempty:Determinewhetherthefunctionisempty

Num2str:Convertavaluetoastring

Thenruntheprogram,keeptyping,theresultsareasfollows:

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


发表评论:

最近发表
网站分类
标签列表