matlab
记录基本的 matlab 语法,便于熟悉 python 语言的人能够快速熟悉 matlab
基础语法
下面以一个 python 脚本转成 matlab 脚本演示基本的语法,分支语句,循环语句,数组,绘图和文件读取操作。
python
import numpy as np
import matplotlib.pyplot as plt
# basic
## IO
a = 1.2
b = 3.2
c = a + b
print(c)
d = int(input('Enter a integer: '))
## for loop
sum = 0
for i in range(1, d + 1):
sum += i
print(sum)
## if statement
if sum > c:
print('sum > c')
else:
print('sum <= c')
# arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b
print(c)
d = a @ b
print(d)
# plot
ax1, ax2 = plt.subplot(211), plt.subplot(212)
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.exp(-x)
ax1.plot(x, y1, label='sin(x)')
ax1.plot(x, y2, label='exp(x)')
ax1.set_title('Sine and Exponential Functions')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.legend()
ax1.grid()
# file operations
x, y = [], []
file = open('data.txt', 'r')
for line in file:
m, n = line.split(',')
x.append(float(m))
y.append(float(n))
file.close()
x, y = np.array(x), np.array(y)
ax2.plot(x, y)
ax2.set_title('Data from file')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.grid()
plt.show()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
matlab
% basic
% IO
a = 1.2;
b = 3.2;
c = a + b;
disp(c);
d = input('Enter an integer: ');
% for loop
sum = 0;
for i = 1:d
sum = sum + i;
end
disp(sum);
% if statement
if sum > c
disp('sum > c');
else
disp('sum <= c');
end
% arrays
a = [1, 2, 3];
b = [4, 5, 6];
c = a + b;
disp(c);
d = dot(a, b);
disp(d);
% plot
x = linspace(0, 10, 100);
y1 = sin(x);
y2 = exp(-x);
% file operations
x = [];
y = [];
fileID = fopen('data.txt', 'r');
while ~feof(fileID)
line = fgetl(fileID);
data = str2double(strsplit(line, ','));
x(end+1) = data(1);
y(end+1) = data(2);
end
fclose(fileID);
x = x(:); % Convert to column vector
y = y(:); % Convert to column vector
% Updated plot with subplots
figure;
ax1 = subplot(2, 1, 1);
plot(ax1, x, y1, 'DisplayName', 'sin(x)');
hold(ax1, 'on');
plot(ax1, x, y2, 'DisplayName', 'exp(x)');
title(ax1, 'Sine and Exponential Functions');
xlabel(ax1, 'x');
ylabel(ax1, 'y');
legend(ax1);
grid(ax1, 'on');
hold(ax1, 'off');
ax2 = subplot(2, 1, 2);
plot(ax2, x, y);
title(ax2, 'Data from file');
xlabel(ax2, 'x');
ylabel(ax2, 'y');
grid(ax2, 'on');1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
其中读取的数据文件 data.txt 如下:
txt
0,1.0806046117362795
1,0.8188911339231568
2,0.5227710795597429
3,0.2099133051785825
4,-0.10135223121115033
5,-0.39294992677039986
6,-0.6479592599200746
7,-0.8514577931662315
8,-0.9912662175839451
9,-1.0585598557495408
10,-1.0483173673696657
11,-0.9595851077296909
12,-0.7955443115782672
13,-0.5633776047067569
14,-0.2739408442937118
15,0.05874448891138273
16,0.41815461138775506
17,0.7860513331765097
18,1.1433574478903998
19,1.471078327838829
20,1.7512261652282919
21,1.9677030740460346
22,2.1071010972102404
23,2.159380965783259
24,2.118397055019963
25,1.9822431151329845
26,1.753401690287374
27,1.4386892884301086
28,1.048998899790005
29,0.5988509376345643
30,0.10577264806638209
31,-0.41046591353380024
32,-0.9287244902818848
33,-1.4274338308148813
34,-1.8855622315288947
35,-2.2835628821439142
36,-2.604258286670709
37,-2.833620871514232
38,-2.961413636643832
39,-2.9816611343935078
40,-2.892928874753533
41,-2.6983980924725595
42,-2.4057322586370438
43,-2.0267413377981907
44,-1.576859133312462
45,-1.0744576933707384
46,-0.5400302664272254
47,0.004719652429932331
48,0.5378413347452862
49,1.0381076793715136
50,1.48594939994432
51,1.8643097780898672
52,2.1593809657832597
53,2.3611883741403368
54,2.4639967892346997
55,2.4665201761441513
56,2.371926281523652
57,2.1876366952504736
58,1.9249325372382953
59,1.5983849528248746
60,1.2251377086900455
61,0.8240760032225956
62,0.414920823115406
63,0.017291549365397008
64,-0.3502191151200744
65,-0.6709145196468667
66,-0.9307670906199411
67,-1.1190589827135453
68,-1.2288606408890306
69,-1.2573252525296912
70,-1.205785787714068
71,-1.0796506590333168
72,-0.8881035423377233
73,-0.6436221402189147
74,-0.361339207513958
75,-0.058276592325509455
76,0.24751097155030652
77,0.5378413347452863
78,0.7953163602511377
79,1.0041957258426017
80,1.15119129223309
81,1.2261439726897148
82,1.2225505786798703
83,1.1379151896309683
84,0.9739078713856589
85,0.7363226592998084
86,0.43483620174599646
87,0.08257788330800919
88,-0.30446882806941256
89,-0.708206058333803
90,-1.1092677638012465
91,-1.4879329577788851
92,-1.825063104564884
93,-2.1030196086245496
94,-2.3065181418707086
95,-2.4233793953784337
96,-2.4451405823172774
97,-2.3674984363494973
98,-2.190562245623235
99,-1.91890427357084261
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100