MATLAB Programming Fundamentals
1. Variable
The variable name must start with a letter, followed by any letter and underscore, but there can be no spaces, Chinese characters, or punctuation. MATLAB divides variables into three categories: local variables, global variables, and permanent variables, and it comes with some special variables. Keywords are process control variables frequently used in program design. There are 20 in total. These keywords cannot be used as variable names.
'break' 'case' 'catch' 'classdef' 'continue' 'else' 'elseif' 'end' 'for' 'function' 'global' 'if' 'otherwise' 'parfor' 'persistent' 'return' 'spmd' 'switch' 'try' 'while'
2. MATLAB control flow
2.1 Sequence structure
A common trilogy program of input, calculation, and output is a sequential structure, which usually composes a complex structure with other structures.
2.2 if structure
If only two sets of commands are executed, as follows:
if expression %判决条件
command1 %条件为真,执行命令1,结束此结构
else
command2 %条件为假,执行命令2,结束此结构
end
If there are n groups of commands executed, as follows:
if expression1 %判决条件
command1 %条件1为真,执行命令1,结束此结构
elseif expression2
command2 %条件1为假,条件2为真,执行命令2,结束此结构
else
commandn %前面所有条件均为假,执行命令n,结束此结构
end
2.3 switch structure
switch value %value为需要进行判决的标量或字符串
case test1
command1 %如果value等于test1,执行command1,结束此结构
case test2
command2 %如果value等于test2,执行command2,结束此结构
case testk
commandk %如果value等于testk,执行commandk,结束此结构
otherwise
command %如果value不等于任何test,执行command,结束此结构
end
2.4 try structure
try
command1 %命令1首先被执行,若正确,执行完成结束此结构
catch
command2 %命令1出错,执行命令2,结束此结构
end
If there is an error in command 1, lasterr can be called to query the cause of the error.
2.5 for loop
for i=array %指定循环变量
command %循环变量每次赋值都执行一次命令
end
2.6 while loop
while expression %判决条件
command %若判决条件为真,继续执行,直到为假,结束循环
end
The while loop does not need to know the number of loops, it only needs to meet certain conditions to end the loop, while the if loop must specify the number of loops, both have their own strengths.
3. Other commonly used instructions to control program flow
Common instruction set |
---|
return, forcibly transfer control |
Input and keyboard, control is temporarily handed over to the keyboard |
yesinput, trust me, basically won’t be used |
pause and pause(n), one is to pause and wait for the user to press any key to continue, the other is to pause for n seconds to continue |
continue, pass control to the next loop iteration, ignoring any remaining statements in the loop body |
break, meet the condition, force to exit the loop |
error and warning, errors and warnings, generally not used |
4. Scripts and Functions
4.1 Script
Scripts are mainly used when there are many command lines, and the variables involved in the script will be reflected in the work area.
4.2 Functions
The biggest difference between a function and a script is that the function can be called (@function name), so the first line of the function file is the function declaration line.
function [输出变量]=函数名(输入变量)
4.3 Anonymous functions and sub-functions
An anonymous function does not have a function name, nor is it a function file. It only contains an expression and input and output parameters. An anonymous function call does not require a function handle, just like the MATLAB built-in function call.
f=@(input1,inputn)expression
As the name implies, sub-functions are relative to the main function. In the same M file, the first function is the main function, and the other functions are sub-functions. All functions require function declaration lines.
4.4 eval and feval functions
eval(s) uses MATLAB's annotator to find s, feval('function name', variable), these two are basically useless.
4.5 Functions of functions
To put it simply, it is a function named as an independent variable. This type of function is often used in optimization, similar to the form of function name (@function name, input parameter).
5. Detection and transmission of variables in M files
5.1 Variable detection instructions
Instruction set | Features |
---|---|
nargin | Get the actual input variables in the function body |
nargout | Get the actual output variable in the function body |
nargin ('fun') | Get the number of nominal input variables of the function specified by fun |
nargout(‘fun’) | Get the number of nominal output variables of the function specified by fun |
varargin | Variable number input variable list |
varargout | Variable number output variable list |
5.2 Cross-space variable passing and assignment
No
6. MATLAB program debugging
Generally speaking, there are only three cases of program errors. Syntax errors and logic errors are the most and there are fewer exceptions. There are also direct and indirect methods for debugging. Personally, I like to compile a program to debug a program, so as to minimize the final error rate.