5.第五天2022-1-23
5.1 脚本编写和注释+变量的输入1
>> rad=input('Enter the radius:')
Enter the radius:5
rad =
5
>> letter=input('Enter the char: ','s') %s表示希望输入的内容被解读成字符
Enter the char: 5
letter =
'5'
>> whos
Name Size Bytes Class Attributes
circle 1x1 8 double
letter 1x1 2 char
%直接输出变量也是可以的
>> R=16
R =
16
>> rad=input('Enter the radius:')
Enter the radius:R
rad =
16
>> letter=input('Enter the char: ','s') %s表示希望输入的内容被解读成字符
Enter the char: %不输入内容,直接回车
letter =
空的 0×0 char 数组
>> letter=input('Enter the char: ','s') %s表示希望输入的内容被解读成字符
Enter the char: go
letter =
' go' %空格会包含在里面
>> letter=input('Enter the char: ','s') %s表示希望输入的内容被解读成字符
Enter the char: 'apple'
letter =
''apple''
>> letter=input('Enter the char: ')
Enter the char: 'apple'
letter =
'apple'
%双引号,字符串
>> letter=input('Enter the char: ')
Enter the char: "apple"
letter =
"apple"
>> whos
Name Size Bytes Class Attributes
letter 1x1 158 string
length=input('Enter the length: ');
units=input('Is that f(eet) or m(eters)?: ','s');
%命令行窗口出现以下结果:
Enter the length: 12.3
Is that f(eet) or m(eters)?: m
>> length
length =
12.3000
>> units
units =
'm'
>> v=input('Enter a vector: ') %输入数组
Enter a vector: [12 4 567]
v =
12 4 567
5.2 变量的输出和显示2
>> test %运行test脚本
the area of the disc is 78.539816
the circle of the disc is 31.415927
>> disp(area) %运用函数输出面积
78.5398
>> disp(letter) %运用函数输出字符串里面的内容
apple
>> fprintf('The value is %d, for sure! \n',4^3) %fprintf输出,%d是占位符,以整数的格式输出,\n是换行
The value is 64, for sure!
>> fprintf('The value is %f, for sure! \n',4^3) % %f数字以浮点数的格式显示出来
The value is 64.000000, for sure!
>> fprintf('The value is %c, for sure! \n',4^3) % %c以char字符的形式输出出来
The value is @, for sure!
>> fprintf('The value is %s, for sure! \n',[64 65]) % s和c的区别是 s一次性输出多个字符 c要一个个输出
The value is @A, for sure!
>> fprintf('The value is %c, for sure! \n',[64 65])
The value is @, for sure!
The value is A, for sure!
>> fprintf('line 1\nline 2\n\nline 4\n') %n是换行
line 1
line 2
line 4
>> fprintf('The length is %f. \nThe unit is %c.\n',length,units) %输出两个或多个变量
The length is 12.300000.
The unit is m.
>> fprintf('|%6d|\n',4^3) % %6d是输出整数,占用6个字符的宽度
| 64|
>> fprintf('|%6d|\n',randi([1,1000],[10,1])) %限制字符的宽度,会显得更清晰
| 815|
| 906|
| 127|
| 914|
| 633|
| 98|
| 279|
| 547|
| 958|
| 965|
>> fprintf('|%8.3f|\n',pi) % %f是浮点数,所以会有小数点,要用两个数字限制,8是字符宽度,3是小数点位数
| 3.142|
>> fprintf('|%8.3f|\n',rand([10,1])+2) %产生浮点数在2-3之间
| 2.158|
| 2.971|
| 2.957|
| 2.485|
| 2.800|
| 2.142|
| 2.422|
| 2.916|
| 2.792|
| 2.959|
>> fprintf('|%-10d|\n',randi([1,1000],[10,1])) %加个减号是左对齐的方式输出
|656 |
|36 |
|850 |
|934 |
|679 |
|758 |
|744 |
|393 |
|656 |
|172 |
>> fprintf('|%+10d|\n',randi([-1000,1000],[10,1])) %%加个加号是显示出数字的正负号输出
| +412|
| -937|
| -446|
| -908|
| -806|
| +647|
| +390|
| -366|
| +901|
| -932|
>> fprintf('|%10s|\n','street') %输出字符长度是10的字符串
| street|
>> fprintf('|%.4s|\n','street') %强制输出4个字符
|stre|
>> fprintf('%11d\n',randi([10^6,10^10],[100,1])+10^10) %就可以生成100个11位的电话号码了
>> fprintf('%f\t%f\t%f\n',rand(),rand(),rand()) %\t是等间距的空格
0.032601 0.561200 0.881867
>> test %运行test脚本
>> fprintf('''\n') %两个单引号打出来,会返回一个单引号
'
>> fprintf('\\\n') %反斜杠是一样的道理
\
%%代码
fprintf('Note: the units will be inches.\n');
radius = input('Please enter the radius: ');
area=pi*radius^2; %求面积
fprintf('for the circle with a radius of %.2f inches,the area of the disc is %.2f inches squared\n',radius,area);
%命令行窗口
>> test2
Note: the units will be inches.
Please enter the radius: 5
for the circle with a radius of 5.00 inches,the area of the disc is 78.54 inches squared
5.3 变量的输出和显示3+基本绘图功能
5.3.1 输出和输入
%%若没规定浮点数的小数位数,则默认是6位
>> fprintf('|%12f|\n',pi)
| 3.141593|
>> fprintf('|%12.6f|\n',pi)
| 3.141593|
%%练习
ch=input('Enter the character: ','s');
num=input('Enter the number: ');
fprintf('the character is %3c.\n',ch);
fprintf('the number is %-8.3f.\n',num);
%测试结果
>> test2
Enter the character: a
Enter the number: 13.2
the character is a.
the number is 13.200 .
5.3.2 绘图
%% 创建(x,y),画出红色的星号
x=11;
y=48;
plot(x,y,'r*'); % r是红色的意思,可以放在*的前面后面都可以
%% 图像中显示的x和y的范围、xlabel、ylabel
%axis([9 12 35 55]); % axis([xmin xmax ymin ymax])
axis([x-2 x+2 y-10 y+10]);%使得点一定落在图的特定范围内
xlabel('Time');
ylabel('Temperature');
%% title
title('Time and Temperature');

x=1:6;
y=[1 5 3 9 11 8];
plot(x,y,'r*--'); % r颜色 *点的形状 --线的形状
hold;
plot(x,x+1,'mx:'); %m是颜色 x是点的形状 :是线的形状 若只想出现点,就‘x’
xlabel('Time');
ylabel('Temperature');
title('Time and Temperature');
[关于hold的解释]

5.4 基本绘图功能2+关于hold的注意事项
%%hold; hold on; hold off
%% 画两张图
x=1:6;
y=[1 5 3 9 11 8];
plot(x,y,'r*--'); % r颜色 *点的形状 --线的形状
%plot呼唤绘图窗口figure 1 就绪,画图
hold on; %接着画
plot(x,x+1,'mx:'); %m是颜色 x是点的形状 :是线的形状
plot(x,x+2,'go-');
hold off; %结束画
xlabel('Time 1');
ylabel('Temperature 1');
title('Time and Temperature 1');
figure(); %呼唤新的绘图窗口figure 2激活
plot(x,x+1,'x-'); %figure 2
xlabel('Time 2'); %figure 2
ylabel('Temperature 2'); %figure 2
title('Time and Temperature 2'); %figure 2
figure(1)%呼叫figure 1激活,此时figure 2 没激活
hold on; % ishold=true, on figure 1
plot(x,x+3,'o-');

close all; %close all figure window(s)
figure(1); %召唤出figure 1
x=1:6;
y=[1 5 3 9 11 8];
plot(x,y); hold on; %接着画
figure(2);
plot(x,y+1,'x-');hold on;
%会出现两张图,如果点击figure 1的图,
clf %clear current figure 清空激活的画布 即清空figure 1 轴、数字和线都没有了
plot(x,y+1,'o:'); %m是颜色 x是点的形状 :是线的形状 在figure 1画
xlabel('Time 1');
ylabel('Temperature 1');
title('Time and Temperature 1');

一个figure上可以有很多画布,每个画布有自己的画布状态
cla清空选中的画布,清空内容,但是轴和数字还在,线没有了
%% cla和一个figure上的两块画布
close all; %close all figure window(s)
figure(1); %召唤出figure 1
x=1:6;
y=[1 5 3 9 11 8];
subplot(2,1,1);
plot(x,y);hold on;
subplot(2,1,2);plot(x,y+1,'o:'); hold on;%m是颜色 x是点的形状 :是线的形状
xlabel('Time 1');
ylabel('Temperature 1');
title('Time and Temperature 1');


5.5 基本绘图功能3+数据的保存和读取1
5.5.1 绘图
>> plot(rand(3,1),'o-') %1线
>> hold on
>> plot(rand(3,1),'o:') %2线
>> plot(rand(3,1),'rx:') %3线
>> plot(rand(3,1),'o:') %4线
>> grid %加入网格
>> grid %关掉网格
>> grid on%强制开启网格
>> grid off%强制关掉网格
>> grid on
>> legend('Line 1','Line 2','Line 3','Line 4') %对线做说明

>> bar(1:10,randi([1,10],[1,10]))%画柱状图。里面的数字表示横纵坐标的范围,x范围1到10,y范围是1行10列的1到10的随机整数
>> hold on
>> plot(1:0.1:10,sin(1:0.1:10),1:0.1:10,cos(1:0.1:10))%两组函数,x范围1到10,间隔0.1,画sin和cos图像
>> xlabel('x');
>> ylabel('y');
>> title('figure 1');
>> legend('bar','Line 2','Line 3')

5.5.2 数据读取写入累加
1.写入
>> save testfile.dat mymat -ascii %-ascii是一个描述性语句,意思是以字符的形式存储 >> type testfile.dat 8.4912931e-01 6.7873515e-01 7.4313247e-01 9.3399325e-01 7.5774013e-01 3.9222702e-01

%%在上面的基础上继续,生成mymat2,执行save语句,会覆盖掉原来的内容
>> mymat2=rand(4,3)
mymat2 =
0.6555 0.2769 0.6948
0.1712 0.0462 0.3171
0.7060 0.0971 0.9502
0.0318 0.8235 0.0344
>> save testfile.dat mymat2 -ascii %-ascii是一个描述性语句,意思是以字符的形式存储
>> type testfile.dat
6.5547789e-01 2.7692298e-01 6.9482862e-01
1.7118669e-01 4.6171391e-02 3.1709948e-01
7.0604609e-01 9.7131781e-02 9.5022205e-01
3.1832846e-02 8.2345783e-01 3.4446081e-02
%%基础上继续,生成mymat3,在原存储基础上添加内容
>> mymat3=rand(4,3)
mymat3 =
0.4387 0.1869 0.7094
0.3816 0.4898 0.7547
0.7655 0.4456 0.2760
0.7952 0.6463 0.6797
>> save testfile.dat mymat3 -ascii -append%-ascil是一个描述性语句,意思是以字符的形式存储,append是在原存储内容的基础上添加
>> type testfile.dat
6.5547789e-01 2.7692298e-01 6.9482862e-01
1.7118669e-01 4.6171391e-02 3.1709948e-01
7.0604609e-01 9.7131781e-02 9.5022205e-01
3.1832846e-02 8.2345783e-01 3.4446081e-02
4.3874436e-01 1.8687260e-01 7.0936483e-01
3.8155846e-01 4.8976440e-01 7.5468668e-01
7.6551679e-01 4.4558620e-01 2.7602508e-01
7.9519990e-01 6.4631301e-01 6.7970268e-01
%%也可以添加不是同一列数的矩阵
>> mymat4=rand(3,4)
mymat4 =
0.6551 0.4984 0.5853 0.2551
0.1626 0.9597 0.2238 0.5060
0.1190 0.3404 0.7513 0.6991
>> save testfile.dat mymat4 -ascii -append%-ascil是一个描述性语句,意思是以字符的形式存储,append是在原存储内容的基础上添加
>> type testfile.dat
6.5547789e-01 2.7692298e-01 6.9482862e-01
1.7118669e-01 4.6171391e-02 3.1709948e-01
7.0604609e-01 9.7131781e-02 9.5022205e-01
3.1832846e-02 8.2345783e-01 3.4446081e-02
4.3874436e-01 1.8687260e-01 7.0936483e-01
3.8155846e-01 4.8976440e-01 7.5468668e-01
7.6551679e-01 4.4558620e-01 2.7602508e-01
7.9519990e-01 6.4631301e-01 6.7970268e-01
6.5509800e-01 4.9836405e-01 5.8526775e-01 2.5509512e-01
1.6261174e-01 9.5974396e-01 2.2381194e-01 5.0595705e-01
1.1899768e-01 3.4038573e-01 7.5126706e-01 6.9907672e-01
%%练习
%提示用户输入矩阵的行数和列数
r=input('Enter the number of rows:');
c=input('Enter the number of columns:');
%创建有上述输入行和列的矩阵,随机整数矩阵,1-100
mat=randi([1,100],[r,c]);
%将上述矩阵写入'randMat.dat'的文件中
save randMat.dat mat -ascii;
%%命令行窗口
>> test2
Enter the number of rows:4
Enter the number of columns:4
>> type randMat.dat
9.0000000e+01 1.5000000e+01 8.2000000e+01 2.0000000e+01
9.6000000e+01 2.6000000e+01 2.5000000e+01 2.6000000e+01
5.5000000e+01 8.5000000e+01 9.3000000e+01 6.2000000e+01
1.4000000e+01 2.6000000e+01 3.5000000e+01 4.8000000e+01
>> test2
Enter the number of rows:3
Enter the number of columns:5
>> type randMat.dat
3.6000000e+01 5.5000000e+01 7.6000000e+01 5.7000000e+01 5.4000000e+01
8.4000000e+01 9.2000000e+01 7.6000000e+01 8.0000000e+00 7.8000000e+01
5.9000000e+01 2.9000000e+01 3.9000000e+01 6.0000000e+00 9.4000000e+01
2.读取
>> load randMat.dat >> type randMat.dat 3.6000000e+01 5.5000000e+01 7.6000000e+01 5.7000000e+01 5.4000000e+01 8.4000000e+01 9.2000000e+01 7.6000000e+01 8.0000000e+00 7.8000000e+01 5.9000000e+01 2.9000000e+01 3.9000000e+01 6.0000000e+00 9.4000000e+01












