MATLAB程序设计详细记录_第三章

12. 变量的输出

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 is 78.5398
% ctrl+r 注释  
% ctrl+t 取消注释
% ctrl+z 撤销
% ctrl+s 保存脚本
fprintf('the area of the disc is %f \n',area); % the area of the disc is 78.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 is 78.53980 
fprintf('the area of the disc is %6.2f \n',78.5398); % the area of the disc is  78.54 
fprintf('the area of the disc is %6.e \n',78.5398); % the area of the disc is 7.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 is 64
fprintf('the value is %c \n',4^3); % the value is @ 
fprintf('the value is %s \n',[64 65]); % the value is @A 
fprintf('the value is %c \n',[64 65]); % 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([9 12 35 55]); % axis([xmin xmax ymin ymax]); % 改变坐标系范围
xlabel('x轴'); % 描述横坐标
ylabel('y轴'); % 描述纵坐标
title('标题'); % 描述图的标题
y = [7 8 5 4 5 9];
plot(y); % x默认依次为1 2 3 4 5 6
% 颜色
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 = [7 8 5 4 5 9];
% ishold = 0
plot(x,y,'k*-'); % k:黑色 *:点 -:线的形状
hold; % 保持某一画布(坐标系)之前的图案,对ishold值翻转,ishold = 1
plot(x,x+1,'k*-'); % k:黑色 *:点 -:线的形状
x = 1:6;
y = [7 8 5 4 5 9];
% 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
x = 11;
y = 48;
plot(x,y,'*r'); % r = 'red',在坐标中画一个红色*
axis([9 12 35 55]); % axis([xmin xmax ymin ymax]); % 改变坐标系范围
xlabel('x轴'); % 描述横坐标
ylabel('y轴'); % 描述纵坐标
title('标题'); % 描述图的标题
figure(); % 重新再打开一个画布,figure2
plot(x,y,'*k'); 
axis([9 12 35 55]); % axis([xmin xmax ymin ymax]); % 改变坐标系范围
xlabel('x轴 2'); % 描述横坐标
ylabel('y轴 2'); % 描述纵坐标
title('标题 2'); % 描述图的标题
figure(1); % 目前有figure1,2; 表示:激活figure1,不激活figure2,此后将在figure1上进行画图
hold on; % ishold = true,保持figure1上原图不变,继续画图
plot(x,y,'y*'); % 在figure1上画图
close all; % 关闭所有figure
clf; % 清空figure中所有画布,即当前figure中可能有多个画布(坐标系),此时ishold = 0(不是false,因为目前没有画布);
close(1);figure(1); % 清空figure1
cla; % 清空某一个画布上的内容
figure();
plot(rand(3,1),'x--'); % Line 1
plot(rand(3,1),'o:'); % Line 2
plot(rand(3,1),'s-'); % Line 3
grid; % 第一次,令坐标系中有网格
grid; % 第二次,取消坐标系中有网格
grid on; % 添加网格
grid off; % 取消网格
legend('Line 1','Line 2','Line 3'); % 描述每条线
bar(1:10,randi([1,10],[1,10])); % 10个柱形块,每块横坐标(位置)1-10,每块的纵坐标(高度)1-10
plot(1:0.1:10,sin(1:0.1:10),1:0.1:10,sin(1:0.1:10)); % 同一画布,画出两个图

16. 数据的保存和读取

% 从文件读取数据
% 写数据到文件
% 已有数据文件添加数据
mymat = rand(2,3);
% testfile.dat 文件名
% mymat -ascii  mymat数据保存形式
save testfile.dat mymat -ascii% 写数据到testfile.dat文件
type testfile.dat; % 将文件中数据显示到matlab
mymat2 = rand(4,3);
save testfile.dat mymat2 -ascii% 写数据到testfile.dat文件,覆盖原文件中数据
mymat3 = rand(4,3);
% -append 在原数据后边添加新数据
save testfile.dat mymat2 -ascii -append;% 写数据到testfile.dat文件,在原数据后边添加新数据
load testfile.dat; % 读取文件中的数据,保存到一个和文件名相同的变量中

17. 单一输出变量函数

% function 返回结果 = 函数名(输入变量)
% end
function outputArgument = functonName(inputArgument) 
end

18. 单一输出变量函数与函数向量化

function area = calcarea(rad)
area = pi * rad ^ 2;
end
calcarea(5); % 正确
calcarea([1 2 3]); % 错误
function area = calcarea(rad)
area = pi * rad .^ 2;
end
calcarea(5); % 参数为标量,正确
calcarea([1 2 3]); % 参数为向量,正确, 3.1416 12.5664 28.2743

function vol = conevol(radius,height)
vol = pi * radius .^2 .* height / 3;
end
conevol(4,6.1); % 参数为标量,102.2065
conevol([1 2],[2 1]); % 参数为向量, 2.0944 4.1888

19. 单一输出变量函数与变量的使用范围、局部函数

19.1 变量的使用范围

function outCost = funCost(a,b,c)
s = a * b;
outCost = s * c;
end
cost = funCost(1,2,3);
 % a b c s outCost属于funCost的Workspace
 % cost 属于调用函数的Workspace

19.2 局部函数

 % 函数调用和定义在同一个文件中,不可被其他文件调用
 a = f1(5);
 function out = f1(b,c)
 s = 2*b*f2(c);
 end
 function out2 = f2(d)
 out2 = 3*d;
 end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值