从上至下的程序设计方式:top-down design
redius = 5;
area = pi * redius ^ 2;
disp(['圆的面积是: ' num2str(area)]);
脚本的命名不能与变量名相同
%this script calculates the circumference of a circle
radius = 5;
circumference = 2*pi*radius;
disp(['圆的周长是: ' num2str(circumference)]);
%{
这里可以加注释
%}
length = input('Enter: ');
unit = input('m or n :','s')
固定长度输出:
fprintf(’|%8.3f\n|’,randi([1,1000],[2,3]))
输出函数:
输出:
matlab画图:
当写成axis = [],之后在调用axis函数时,会被变量占用无法使用,出现错误。
axis([x-2 x+2 y-2 y+2]);将横纵坐标与点的位置挂钩
不指定坐标matlab会自动包括想画的所有坐标范围。
matlab常用颜色代码:
b;
g;
r;
c;
m;
y;
k;
w;
matlab线段代码:
matlab可以设置断点调试,在编辑左边
再画一个plot,后一个plot会将前一个plot替换掉,可以使用hold改变。
hold擦画布和不擦画布
hold是将擦画布的状态和不擦画布的状态进行转换,只需要使用一次。默认是擦画布的状态:ishold = False
figure()控制画布打开的数量
%figure
close all; %八把已经存在的画布关掉
x = 1:6;
y = [2 5 7 8 11 5];
hold on;
plot(x,y);
xlabel('Time');
ylabel('Temp');
title('Time and Temp');
%将重新开启新的绘图窗口
figure();
plot(x,x+1,'x-');
xlabel('Time2');
ylabel('Temp2');
title('Time and Temp2');
figure(1);
plot(x,x+3,'o-');
clf:清空画布
也可以close(1);然后figure(1);
%figure
close all; %八把已经存在的画布关掉
x = 1:6;
y = [2 5 7 8 11 5];
hold on;
plot(x,y);
figure(2);
plot(x,y,'*');
clf;
plot(x,y+1,'o-')
xlabel('Time');
ylabel('Temp');
title('Time and Temp');
subplot()在一块画布上面画多个图
grid调整是否开启网格
grid on一直开启网格
bar(1:10,randi([1,10],[1,10]));画柱状图
close all;
figure;
hold on;
plot(rand(3,1));
plot(rand(3,1),'*--');
plot(rand(3,1),'o:');
grid on
legend('line1','line2','line3')
bar(1:10,randi([1,10],[1,10]));
plot(1:0.1:10,sin(1:0.1:10),1:0.1:10,cos(1:0.1:10))
数据存储与读取:
clear
clc
mymat = rand(2,3);
save testfile.dat mymat -ascii
save testfile2.txt mymat -ascii
读取数据:load 文件名
clear
clc
load Utest.dat;
aUtest = Utest(1,:);
bUtest = Utest(2,:);
plot(aUtest,‘ko’);
hold on
plot(bUtest,‘k*’);
xlabel(‘Quter’);
ylabel(‘Sales billion’);
title(‘ABC’);
legend(‘Divsion A’,‘Divsion B’);
写函数:
function outputArgument = function(inputArgument)
outputArgument :输出变量的名字
inputArgument:输入的名字
function area = calcarea(rad)
area = pi * (rad ^ 2);
end
在matlab中,只要函数能够接收标量,就一定能够接收向量
在matlab中,end作为一个结尾,要配对使用
num = input('enter:');
if num <0
disp('输入的是负数,取绝对值');
num = abs(num);
end
fprintf('圆的面积是:%.2f\n',calcarea(num));
返回一个数组
if后必须是逻辑变量
除了0不会被转换成逻辑false,其他数字会被转换成逻辑true
if语句:
function AutoReply()
reply = input('Y/N:','s');
if reply =='Y' || reply == 'y'
disp('接收');
end
if reply == 'N'|| reply == 'n'
disp('不接受');
end
end
if condition
action
end
if-else的matlab结构
function rand_reply()
if rand() < 0.5
disp('正面');
else
disp('反面');
end
end
function casecure()
x = 56;
if x < -1
y = 1;
else
if x<=2
y = x^2;
else
y =x + 40;
end
end
disp(y);
end
function casecure()
x = 56;
if x < -1
y = 1;
elseif x<=2
y = x^2;
else
y =x + 40;
end
disp(y);
end
当定义的函数有返回值时,必须要用到返回值
function outtype = funartype(inputtarg)
[r,c] = size(inputtarg);
if r == 1 && c == 1
outtype = '标量';
elseif r ==1 && c>1
outtype='行向量';
elseif r >1 && c == 1
outtype = '列向量';
else
outtype='矩阵';
end
end
function letCrade = letterCrade(numGrade)
if numGrade == 10 || numGrade == 9
letCrade = 'A';
elseif numGrade == 8
letCrade = 'B';
else
letCrade = '其他';
end
end
根据一个确定的值来进行判断
function letCrade = letterCrade(numGrade)
switch numGrade
case 10
letCrade = 'A';
case 9
letCrade = 'A';
case 6
letCrade = 'C'
otherwise
letCrade = 'F';
end
end
和if-else一起使用
function letCrade = letterCrade(numGrade)
if numGrade < 0 || numGrade > 100
letCrade = 'X';
else
switch numGrade
case 10
letCrade = 'A';
case 9
letCrade = 'A';
case 6
letCrade = 'C';
otherwise
letCrade = 'F';
end
end
end
isa:判断变量属于什么类型
iskeyword():判断是否是关键字
循环语句:
for i = 1:5 %循环遍量和循环范围
fprintf(‘loop %d\n’,i);
end
for i = 1:5
fprintf('%s\n','*');
end
可随意安排你的画布画的图形
subplot(n,m,k)
循环画图
n = 3;
for i = 1:n
x = 0:0.01:1;
y = exp(2^i*x);
plot(x,y,'LineWidth',5,'DisplayName',['exp(2^' num2str(i) 'x)']);
hold on;
end
xlabel('x');
ylabel('y');
legend('show');
两个循环修改矩阵里面的内容:
rows = 4;
colums = 5;
mat = NaN(rows,colums);
for i = 1:rows
for j = 1:colums
mat(i,j) = i*j;
end
end
嵌套循环
load testfile2.dat;
[r,c] = size(testfile2);
for row = 1:r
runsum = 0;
for col = 1:c
if testfile2(row,col) > 0
runsum = runsum + testfile2(row,col);
end
end
fprintf('%d的和%d\n',row,runsum);
end
while循环结构
while condition
action1
action2
end
high = 9;
i = 0;
fac= 1;
while fac <= high
i = i+1;
fac = fac * i;
end
fprintf('%d\n',fac);
load newSt.dat
i = 1
while newSt(i) ~= -99
i = i +1;
end
validData = newSt(1:i-1);
plot(validData,‘ko’);
counter = 0;
inputnum = input('Enter:');
while inputnum > 0
fprintf('% \n',inputnum);
counter = counter + 1;
inputnum = input('Enter:');
end
fprintf('%d\n',counter);
while结合for循环
n = 3;
for i = 1:n
inputnum = input('Enter:');
while inputnum < 0
fprintf('%f\n',inputnum);
inputnum = input(['Enter:[#' num2str(i) ']:']);
end
fprintf('OK\n');
end
测量函数的运行时间
tic;测量的开头,测量的结束
向量运算更加高效
M = rand(10,10);
SumCol = zeros(1,10);
tic;
for iCol = 1:10
for iRow = 1:10
SumCol(iCol) = SumCol(iCol) + M(iRow,iCol);
end
end
toc;