disp用於直接在命令行中顯示;sprintf生成帶格式的字符串,通常並不直接用來顯示在命令行;fprintf可以用來直接進行文件的寫入,如果不指定文件的fid則輸出到命令行中。
- disp (display)
% 功能:顯示變量的值
% 特點:僅接受一個輸入
% 格式:disp(X)
% 例子
>> X = [1 2; 3 4];
>> disp(X)
1 2
3 4
- sprintf (string print format)
% 功能:將數據格式轉化為字符串
% 格式:str = sprintf(formatSpec, A1, ..., An)
% 例子
>> formatspec = 'rectangle: height is %d, width is %d, and area is %d.';
>> h = 10;
>> w = 5;
>> s = sprintf(formatspec, h, w, h*w);
>> s
s =
rectangle: height is 10, width is 5, and area is 50.
- fprintf (file print format)
% 功能:將數據按照指定的格式輸出到文本文件中,fid缺省則輸出到屏幕
% 格式:fprintf(fid, format, variables)
% 注意:輸出的時候要在內容末尾添加\n
% 例子
>> a = 10.1;
>> b = 5;
>> fprintf('a: %.1f, b: %d\n', a, b);
a: 10.1, b: 5