Format long:显示小数点后15位
Format short:显示小数点后4位
Format bank:显示小数点后2位
Format +:显示矩阵中元素的正负号
Format rat:用分数的形式显示元素
format short e:1.2346e+001
disp([‘the values in the x matrix are: ‘,num2str(x)]) % the values in the x matrix are: 1 2 3 4 5
若提示输入的句子太长,则可以采用如下形式将句子分为若干行显示:
disp(‘this program calculates the day of year given the ‘);
disp(‘special date.’);
显示结果:
this program calculates the day of year given the
special date.
disp(‘一个字符串’)
disp([‘多个字符串’]): disp=([‘Alice is ’ num2str(12) ’ years old!’])
fprintf:格式化输出
支持的输出格式:
%f: 定点格式
%e: 指数格式
%d: 十进制格式(若显示的是整数,则不包括小数点后的零;若是小数,则以指数形式显示)
%g: 以%f和%e中最紧凑的形式显示
%c: 字符格式(每次显示一个字符)
%s: 字符串格式(显示完整的字符)
\n: 换行(使用fprintf可以在同一行中显示字符和数字信息)
\r: 回车
\t: 表格
\b: 退格
例如:fprintf(‘the sum is: %f\n’,sum_x)