由于基础在,大概过一遍基础。
文章目录
格式化文本输出
i=10;
sprintf("数字是%d",i)
i=10;
sprintf("数字是%f",i)
str='川川';
sprintf(str)
switch
score=input('请输入分数');
if score<0 || score >100
error('输入信息错误');
end
switch score
case {0,60}
res='不及格';
case {61,80}
res='良好';
otherwise
res='优秀';
end
disp(res);
if
%randi均匀分布的伪随机整数
a = randi(250,12,2) % 产生1到250,12*2的数组
pr = 0; %初始值为0
bonus = 10000;
% numel返回元素的数量
for k = 1:numel(a)
pr = pr + a(k);
end
% 上述进行累加
disp(['你一年内完成的工作总时数是: ',num2str(pr)]) % 数字转字符输出
if pr >= 2800
disp(['你的奖金是. ',num2str(bonus)])
else
disp(['希望你明年更好....'])
end
while
k = 1;
while k
if randi(50,1) == 5
disp(['10的位置找到,在 ',num2str(k),' step'])
break
end
k = k + 1;
end
a = 10;
% 循环到20
while( a < 20 )
fprintf('值为: %d\n', a);
a = a + 2;
end
嵌套循环
for i=2:100
for j=2:100
% ~A 等价于 not A
% mod 返回 i 除以j 后的余数
% 检查i是否能被j整除
if(~mod(i, j))
break; % 如果是整数,则不是质数
end
end
if(j > (i/j))
fprintf('%d 是质数\n', i);
end
end
break
a = randi(100,6,6)
k = 1;
while k
disp('程序在执行...')
if a(k) == 27
disp('序遇到数字27,对当前程序没有用处;')
disp(['位置.:',num2str(k)])
disp('因此结束该操作')
break
end
k = k+1;
disp(k);
end
continue
% 程序打印所有能被 3整除的数字 并跳过剩余的
a = (1:4:50);
% 前元素 a(k) 是否为 3 的倍数,即 rem(a(k), 3) == 0。如果不是,继续循环。
for k = 1:numel(a)
% rem(a,b) 返回 a 除以 b 后的余数
% 能整除返回0;否则不是0
if rem(a(k),3)
continue
end
disp(a(k))
end
多重判断:
% 找到能同时整除这里面所有的数字
v = [2,3,4,5,6,7,8,9];
% 设置范围
min = 1;
max = 10000;
for m = min : max
if mod(m,v(1))
continue
else
if mod(m,v(2))
continue
else
if mod(m,v(3))
continue
else
if mod(m,v(4))
continue
else
if mod(m,v(5))
continue
else
if mod(m,v(6))
continue
else
if mod(m,v(7))
continue
else
if mod(m,v(8))
continue
else
disp(['divisible by all :' num2str(m)])
end
end
end
end
end
end
end
end
end
disp('....')
Try - catch
% 使用该语句找错误
a = ones(4);
b = zeros(3);
try
c = [a;b];
catch ME
disp(ME)
end
普通函数
格式:
function [输出变量] =function_name(输入变量);
函数名,必须与文件名一致。
例如:
function f = fact(n)
f = prod(1:n); %prod(A)返回A数字的乘积
disp(f);
end
匿名函数
格式:
函数名 = @(变量)表达式;
当个变量:
cirarea = @ (radius) pi * radius * 2;
cirarea(4)
cirarea(1:4)
FA= @ (x) exp(x^2)/sqrt(x^2+5);
FA(2)
两个变量:
HA = @ (x, y) 2*x^2 - 4*x*y + y^2 ;
HA(5,2)
二维画图
绘制单个图:
% 两个数组绘图,需要长度一致
a = [1, 2, 3, 4, 5];
b = [0, 1, -1, 1, 0];
plot(a, b)
% 绘制sin函数
a = 0:pi/100:3*pi;% 0到3pi之间范围,步长pi/100
b = sin(a);
plot (a, b)
xlabel('x = 0:2\pi') % x标签
ylabel('Sine of x') % y标签
title('绘制sin函数') % 标题
x = [1:10];
y = [58.5, 63.8, 64.2, 67.3, 71.5, 88.3, 90.1, 90.6, 89.5, 90.4];
plot(x, y,':ok')
绘制多个图:
clc
clear all
a = 0: pi/100:2*pi; % 设置变量范围
b=sin (a);
c = cos (a);
plot (a, b,a,c,'r:');%绘制两个
legend ('Sin(a)', 'Cos(a)');%添加示例
xlabel('x') % x标签
ylabel('y') % y标签
title('sin/cos函数') % 标题
修改线条绘制样式:
clc
clear all
a = 0: pi/100:2*pi; % 设置变量范围
b=sin (a);
c = cos (a);
plot (a, b,'--',a,c,'r-o');%绘制两个
legend ('Sin(a)', 'Cos(a)');%添加示例
xlabel('x') % x标签
ylabel('y') % y标签
title('sin/cos函数') % 标题
clc
clear all
x=0:pi/5;3*pi;
[X,Y,Z]=cylinder(5*cos(x));
subplot (2,2, 1)
mesh(X);title('X');
subplot (2,2, 2)
mesh(Y);title('Y');
subplot (2,2, 3)
mesh(Z);title('Z');
subplot (2,2, 4)
mesh([X,Y,Z]);title('XYZ');
如下:
UI
文件
GUI系统包含两种文件,一种是.fig文件,用于设计页面,另一种是.m文件,用来存放控件操作。
创建fig文件
guide
保存.fig文件时,会自动生成相对应的.m的文件。可以在.fig文件里增加修改控件、设计页面,在.m文件里编写逻辑代码。
.fig的页面设计
左侧的控件栏里面直接可通过拖拽放入控件
第一个:按钮
双击拖拽出来的按钮,可进入按钮的检查器,在检查器里设置按钮的属性。
参数 | 含义 |
---|---|
Tag | 控件的名称(不能重名),.m通过Tag来唯一寻找该控件 |
String | 控件里面的文字 |
FontSize | 控件里面的文字的字体大小 |
Visible | 控件初始是否可见 |
Callback | 调用函数,双击中间的书写按钮matlab会自动跳转到相应的.m中的调用函数的位置 |
tag说明:
同理有:可编辑文本
面板:用于设计界面具体大小。
比如:
运行
文字传递案例
界面设计如下:
右键按钮,查看回调,callback
编写如下
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
str=get(handles.edit1,'string'); %获取edit1的字符串
set(handles.edit2,'string',str); % 将获取到的str字符串赋值给edit2
效果如下:
GUI界面大小调整
made by CSDN:川川菜鸟
后续学习内容:Simulink