Matlab Simulink中的一些记录
一、Simulink快捷操作
1、simulink中【复制模块】的快捷方式:”光标选中模块,然后按住Ctrl,拖动模块“即可完成复制。
二、Matlab输出变量
说明:输出Matab中的变量,可用于Matlab程序调试或者Simulink中的Test Sequence单元测试。
1、disp函数
- 输出变量值
clc;
clear all;a = 123;
b = 'hello, world';
c = 123.123;
d = [1 2 3; 4 5 6];
% 输出数据
disp(a);
disp(b);
disp(c);
disp(d);
输出结果:
- 打印变量类型
clc;
clear all;a = 123;
b = 'hello, world';
c = 123.123;
d = [1 2 3; 4 5 6];
% 打印数据类型,结合class函数
disp(class(a));
disp(class(b));
disp(class(c));
disp(class(d));
输出结果:
2、fprintf函数
fprintf函数可以格式化输出变量
clc;
clear all;a = 123;
b = 'hello, world';
c = 123.123;
% 输出变量值
fprintf('a=%d\n', a);
fprintf('b=%s\n', b);
fprintf('c=%f\n', c); %
fprintf('c=%0.2f\n', c); % 保留小数点后2位
fprintf('c=%0.0f\n', c); % 保留整数部分
输出结果:
三、其它函数
1、class函数
class函数 返回变量的类型名称
clc;
clear all;a = 123; % 数值型(默认为 double)
b = 'Hello'; % 字符数组(char)
c = struct('x', 1); % 结构体(struct)
d = {1, 2}; % 单元格数组(cell)disp(class(a)); % 输出 'double'
disp(class(b)); % 输出 'char'
disp(class(c)); % 输出 'struct'
disp(class(d)); % 输出 'cell'
输出结果:
2、isa 函数
isa函数 用于判断变量是否属于特定类型,返回逻辑值 true 或 false。
clc;
clear all;value = 42;
is_double = isa(value, 'double'); % true
is_char = isa(value, 'char'); % false
disp(is_double);
disp(is_char);
输出结果: