MATLAB中的脚本和函数有什么区别? [英] What's the difference between a script and a function in MATLAB?

查看:620
本文介绍了MATLAB中的脚本和函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

这个页面说,



< blockquote>

脚本与功能



脚本是包含MATLAB
语句的m文件。 MATLAB``functions''是
另一种类型的m文件。脚本和
函数之间最大的
差异在于函数输入
和输出参数。脚本文件
只能对
在其m文件中进行硬编码的变量进行操作。作为
你可以看到,功能更多
灵活。因此,他们更多的是
适用于将用于不同的
数据的通用任务



脚本对于不更改的任务
很有用。他们也是一种
的方式来记录
操作的特定顺序,例如使用
特殊参数值的函数调用,可能是
难以记住。



脚本和
函数之间有更多的
微妙差异。脚本可以被认为是
作为键盘宏:当您键入
的脚本名称时,其中包含的所有
命令都将执行
,就像您已经在命令窗口中键入这些
命令。
因此,
脚本中创建的所有变量都将添加到当前会话
的工作空间中。此外,如果
脚本
文件中的任何变量与
中当前工作空间中的任何变量名称相同,那么工作空间中
的值就是
由脚本中的操作更改。
这可以用于您的优势。它
也可能导致不必要的副作用。



相比之下,函数变量为
为函数本地。 (
的异常是可以声明,
使用全局变量,但是
需要
用户的显式操作。)函数
的本地范围变量给你更大的安全性
和灵活性。唯一的方法(除了
明确声明的全局变量)
以获取信息进出
函数是通过参数列表中的
变量。


示例



脚本和一个功能是访问工作区中的变量。例如,假设在工作空间中,您已经定义了两个变量 a = 10 b = 20 。这些变量在主提示符的命令行中定义。



脚本文件 - display_mult.m



disp(a * b);



打字 display_mult 将在工作区中显示 a b 的产品,即 10 * 20 200



但是如果您定义了一个名称为display_mult的函数,该文件的名称相同:



函数文件 - display_mult.m strong>

  function display_mult(a,b)
disp(a * b);
end

您必须将两个变量作为参数包含在函数调用中。因此, display_mult 工作,因为 a b 不存在于函数的工作空间中。您将不得不通过运行 display_mult(a,b)来显示所需的结果。



简单说明



脚本中的每个语句都等同于在MATLAB的命令窗口中键入它们。您只是将它们存储在文件中!



另一方面,一个函数接受参数,是一个新工作区,与主要工作区。



注意:在函数调用结束时, end 可选,但我喜欢添加它来使事情组织。当然,如果文件中有多个函数定义,则它们都必须以 end 结尾。此外,您不能在同一个文件中具有脚本和功能定义。


What are the differences between a MATLAB script file and a MATLAB function file?

解决方案

This page says,

Scripts versus Functions

Scripts are m-files containing MATLAB statements. MATLAB ``functions'' are another type of m-file. The biggest difference between scripts and functions is that functions have input and output parameters. Script files can only operate on the variables that are hard-coded into their m-file. As you can see, functions much more flexible. They are therefore more suitable for general purpose tasks that will be applied to different data.

Scripts are useful for tasks that don't change. They are also a way to document a specific sequence of actions, say a function call with special parameter values, that may be hard to remember.

There are more subtle differences between scripts and functions. A script can be thought of as a keyboard macro: when you type the name of the script, all of the commands contained in it are executed just as if you had typed these commands into the command window. Thus, all variables created in the script are added to the workspace for the current session. Furthermore, if any of the variables in the script file have the same name as the ones in your current workspace, the values of those variables in the workspace are changed by the actions in the script. This can be used to your advantage. It can also cause unwanted side effects.

In contrast, function variables are local to the function. (The exception is that it's possible to declare and use global variables, but that requires and explicit action by the user.) The local scope of function variables gives you greater security and flexibility. The only way (besides explicitly declared global variables) to get information into and out of a function is through through the variables in the parameter lists.

Example

One of the main differences between a script and a function is access to variables in the workspace. For example, suppose in the workspace, you've defined two variables a = 10 and b = 20. These variables are defined at the command line of the main prompt.

Script file - display_mult.m

disp(a*b);

Typing display_mult would display the product of a and b in the workspace, i.e. 10*20 or 200.

But if you defined a function, called display_mult defined in a file with the same name:

Function file - display_mult.m

function display_mult(a,b)
   disp(a*b);
end

You would have to include the two variables as arguments to the function call. So, display_mult would not work this time since a and b does not exist in the function's workspace. You would have to include them by running display_mult(a,b) which will display the desired result.

Simple explanation

Each statement in a script is equivalent to typing them out at the command window of MATLAB. You're just storing them before-hand in a file!

A function, on the other hand, takes in arguments and is a "new" workspace, separate from the main workspace.

Note: The end at the end of a function call is optional but I like to add it to make things organized. Of course, if you have multiple function definitions in a file they will all have to end with end. Also, you cannot have a script and a function definition in the same file.

这篇关于MATLAB中的脚本和函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆