The dry goods of MATLAB (2)

The most dry goods of MATLAB (two)-structure part

咚咚咚咚咚! ! I'm here. In
this section, it's our turn to play the structure of matlab ! Enough for this time. Sit down.
Speaking of structure, every language will learn, and the code division of each language is slightly different. Here today, I will only tell you the difference between the structure of matlab and other languages. I will take the same place, the same place in one stroke~ No
more than Bibi, go you——

a = 5;
x = [1,2];
y = [3,4];

1. Select the structure

(1) %if-elseif-else-end

if a>0
	disp(x);
elseif a==0
	disp(a);
else
	disp(a-1);
end

Differences from other languages:
① There are no parentheses after if;
② There is no need to wrap it with {};
③ There is no space between elseif;
④ Finally, it ends with end.

(2) %switch-case-otherwise-end

switch a
	case 0
		disp(a);
	case 1
		disp(a+1);
	otherwise
		disp('aaa');
end

Differences from other languages:
①There is no parentheses after
switch ; ②There is no colon in the case line ; ③Pay
attention to the indentation of the code;

(3) %try-catch

try
	z = x*y;
catch
	z = x.*y;  %try出错,则执行
end
disp(z);

2. Loop structure

(1)% for loop variable = initial value: step length: final value-end

for i=0:1:10  % 步长为负,则初值大于终值
	disp(i);  % 循环体内不可对循环变量做修改
end

(2) % while-end

 while a>2
 	disp(a);
 	a = a-1;
 end

3. Program control

%continue skips the remaining statements in the
current loop and enters the next loop %break breaks out of the current loop
%return jumps out of the program and returns.
These other languages ​​are the same, and I will learn them, so I won't repeat them.

Four.m file

(1) Script file : A file that cannot be called directly. It is executed according to the script flow from 1 to the last line; there is no input and output parameters, after execution, the variable result returns to the workspace and can be run directly.

The following is the script file, the file name is assumed to be exp.m
%************************************* *********

clear
r = 5;
s = pi*r*r;
p = 2*pi*r;
disp(s)
disp(p)

The following is the call

exp

%**********************************************

(2) Function file : It starts with function and has input and output. Variables are local variables and do not return to the working space. They need to be called.
The following is the function file
%********************************************* *

 function [s, p] = circ(r)  % 文件命名应与函数名一致,系统找文件名circ.m

CIRC calculation of circle area and perimeter
% Simple description
Parameters: input parameter r: circle radius; output parameter s: circle area, p: perimeter
% Detailed description

s = pi*r*r;
p = 2*pi*r;
end

The following is the call

 [a, b] = circ(5);  % 返回为多个参数时,若写a = circ(5)则保留第一个返回值

%**********************************************

At this point, I have finished the structure part of matlab. The dry goods are full, all of which are the points of attention in matlab. I also pointed out the differences with other languages ​​in the article, and everyone needs to pay special attention. The codeword my brother is working on is to help everyone. If you have any questions or do not understand, please leave a comment and correct me.
In the next section, I will bring you all about drawing problems in matlab , let ’s look forward to it~
Goodbye

Guess you like

Origin blog.csdn.net/weixin_49005845/article/details/109671129