首页 > 常识 >

m文件分为哪两类(Matlab基础知识学习)

  • 100次浏览     发布时间:2024-09-02 09:04:22    


本期小编与大家一起来学习Matlab中的脚本代码部分的内容,Matlab既可以当作高级计算器来使用,更是一种强大的编程语言,也是一个交互式的计算环境。建立脚本,用编程语言可以完成更加复杂的操作和命令。

In this issue, the editor will learn the content of the script code part of Matlab together with you. Matlab can be used as an advanced calculator, a powerful programming language, and an interactive computing environment. Create scripts, and use programming languages to complete more complex operations and commands.


用MATLAB语言编写的程序,称为M文件。M文件可以根据调用方式的不同分为两类:命令文件(Script File)和函数文件(Function File)。


Programs written in MATLAB language are called M-files. M files can be divided into two categories according to different calling methods: command files (Script File) and function files (Function File).


其中,命令文件即是脚本文件。所谓脚本文件(命令文件),就是由一系列的MATLAB指令和命令组成的纯文本格式的M文件,执行脚本文件时,文件中的指令或者命令按照出现在脚本文件中的顺序依次执行。脚本文件没有输入参数,也没有输出参数,脚本文件处理的数据或者变量必须在MATLAB的公共工作空间中。


Among them, the command file is the script file. The so-called script file (command file) is a plain text M file composed of a series of MATLAB instructions and commands. When the script file is executed, the instructions or commands in the file are executed in the order in which they appear in the script file. The script file has no input parameters and no output parameters. The data or variables processed by the script file must be in the public workspace of MATLAB.

1

新建脚本

新建脚本操作:点击右上角‘新建脚本’(New Script)快捷键Ctrl+N


New script operation: Click the shortcut key of 'New Script' (New Script) in the upper right corner Ctrl+N



这时我们就可以在里面写命令了,然后输入后就可以保存了(文件命名规则:只能包括字母,数字和下划线)


At this time, we can write commands in it, and then save them after entering them (file naming rules: only letters, numbers and underscores can be included)



脚本编写问题:在每个语句后需要加';',但是没有C/C++严格要求,不加';'也不会报错,但是运行时会直接输出,不加分号时(赋值符号会有高亮显示):


Scripting problem: ';' needs to be added after each statement, but there is no C/C++ strict requirement, no error will be reported without ';', but it will be output directly at runtime, without semicolon (assignment symbol will have Highlight):



加分号时(加分号赋值符号取消赋值高亮):


When adding a semicolon (the semicolon assignment symbol cancels the assignment highlight):



2

基础语句

2.1判断语句

代码是一行一行向下逐步运行的,脚本上有很多代码,当我们想要代码跳过某些行的运行直接运行下面的代码时,我们可以采用Matlab中的判断语句。


The code is run down line by line step by step. There is a lot of code on the script. When we want the code to skip the running of some lines and directly run the following code, we can use the judgment statement in Matlab.


无论在何种编程语言下,都有判断语句,而无论是在C/C++/Python中都有一个相同的名字,叫做if语句,但是在语法结构上有着一些差异。在Matlab中,在if后空格加上条件,如果条件满足,就执行if语句下的语句,else语句后不需要添加条件,else语句指的是不满足if条件的情况下,就执行else语句下的语句。


Regardless of the programming language, there are judgment statements, and whether they are in C/C++/Python, they have the same name, called if statements, but there are some differences in the syntax structure. In Matlab, a space is added after the if condition. If the condition is satisfied, the statement under the if statement is executed. There is no need to add the condition after the else statement. The else statement refers to the case where the if condition is not satisfied, execute the else statement. statement.


值得注意的是,在Matlab中,在完成了if……else……语句后,还应该加上end语句,代表着判断依据的结束。


It is worth noting that in Matlab, after the if...else... statement is completed, the end statement should be added, which represents the end of the judgment basis.

我们下面使用if……else……语句完成一个判断输入数字正负的脚本,来进行一些展示。


Let's use the if...else... statement to complete a script that judges whether the input number is positive or negative for some display.



2.2循环语句

for循环结构中,需要设定一定的循环条件,Matlab根据设定的循环次数执行循环体内的命令。

for x = array

commands

end

In the for loop structure, certain loop conditions need to be set, and Matlab executes the commands in the loop body according to the set number of loops.

for x = array

commands

end


其中,x是循环变量,array是条件数组,commands是要执行的循环代码。循环体的执行次数由array决定。示例如下:


where x is the loop variable, array is the conditional array, and commands is the loop code to execute. The number of executions of the loop body is determined by array. An example is as follows:

2.3条件循环语句

while循环结构对循环体进行无限次的循环运算,直到循环体满足循环结束条件,或达到一定的循环次数后终止。

while expression

commands

end

The while loop structure performs infinite loop operations on the loop body until the loop body satisfies the loop end condition, or terminates after reaching a certain number of loops.

while expression

commands

end


其中,expression是条件表达式,一般情况下,expression的计算结果是一个标量,但也可以是一个数组表达式。当标量结果为true时,循环体一直被执行下去;当expression的结果是一个数组时,只有当数组中所有元素均为true时,循环体才会被执行。示例如下:


Among them, expression is a conditional expression. In general, the result of the calculation of expression is a scalar, but it can also be an array expression. When the scalar result is true, the body of the loop is always executed; when the result of expression is an array, the body of the loop is executed only when all elements in the array are true. An example is as follows:



热门文章
最新文章