radius =5;
area = pi * radius^2;%78.5398
disp(['the area of the disc is ' area]);% 输出函数,the area of the disc is N ,double('N')=78
char(78.5398);%'N'
num2str(area);%'78.5398',把双精度变量转换成字符串
num2str(area,4);%'78.53',保留4位
num2str(area,'%8.2f');%'78.53'
disp(['the area of the disc is ' num2str(area)]);% the area of the disc is78.5398
fprintf('the area of the disc is %f \n',area);% the area of the disc is78.5398% \n表示换行
%%f浮点数占位符
%%.5f保留五位小数
%%6.2f 6指宽度,.2指保留2位小数
%%e 以科学计数法形式显示,
%%d 以整数形式
%%c 以char字符形式输出
%%s 以string形式
fprintf('the area of the disc is %.5f \n',78.5398);% the area of the disc is78.53980
fprintf('the area of the disc is %6.2f \n',78.5398);% the area of the disc is78.54
fprintf('the area of the disc is %6.e \n',78.5398);% the area of the disc is7.85398e+01
13. 变量的输入
rad =input('Enter the radius: ');
rad =input('Enter the radius: ');% 获取用户输入的内容。默认是双精度,
rad =input('Enter the char: ','s');%设置字符形式's',或者输入字符串的时候带引号。
14. 变量的输出
radius =5;
area = pi * radius^2;%78.5398
disp(area);%78.5398,返回变量area中的值
fprintf('the value is %d \n',4^3);% the value is64
fprintf('the value is %c \n',4^3);% the value is @
fprintf('the value is %s \n',[6465]);% the value is @A
fprintf('the value is %c \n',[6465]);% the value is @ , the value is A
fprintf('|%5d| \n',4^3);%|64|,右对齐
fprintf('|%-5d| \n',4^3);%|64| ,左对齐
fprintf('|%+5d| \n',4^3);%|+64| ,正数前面加“+”
fprintf('|%s| \n','street');%|street|
fprintf('|%10s| \n','street');%| street|
fprintf('|%.4s| \n','street');%|stre|
fprintf(' '' \n');% ',输出单引号
fprintf(' \\ \n');% \,输出反斜杠
15. 基本绘图
x =11;
y =48;
plot(x,y,'*r');% r ='red',在坐标中画一个红色*
axis([9123555]);% axis([xmin xmax ymin ymax]);% 改变坐标系范围
xlabel('x轴');% 描述横坐标
ylabel('y轴');% 描述纵坐标
title('标题');% 描述图的标题
y =[785459];
plot(y);% x默认依次为123456
% 颜色
b % blue
g % green
r % red
c % cyan
m % megenta
y % yellow
k % black
w % white
% 点的形状
help plot % 命令行命令,返回相关知识点
.% point
o % circle
x % x-mark
+% plus
*% star
s % square
d % diamond
v % down triangle
^% up triangle
<% left triangle
>% right triangle
p % pentagram
h % hexagram
x =1:6;
y =[785459];% ishold =0
plot(x,y,'k*-');% k:黑色 *:点 -:线的形状
hold;% 保持某一画布(坐标系)之前的图案,对ishold值翻转,ishold =1
plot(x,x+1,'k*-');% k:黑色 *:点 -:线的形状
x =1:6;
y =[785459];% ishold = false
hold on;% ishold = true
plot(x,y,'k*-');% 图1
plot(x,x+1,'k*-');% 图1+ 图2
plot(x,x+2,'k*-');% 图1+ 图2+ 图3
hold off;% ishold = false
plot(x,x+3,'k*-');% 图4
文章目录12. 变量的输出13. 变量的输入14. 变量的输出15. 基本绘图16. 数据的保存和读取17. 单一输出变量函数18. 单一输出变量函数与函数向量化19. 单一输出变量函数与变量的使用范围、局部函数19.1 变量的使用范围19.2 局部函数12. 变量的输出radius = 5;area = pi * radius^2; % 78.5398disp(['the area of the disc is ' area]); % 输出函数,the area of the disc is N