Edit one_more Within editor window: function a = one_more x = input(‘Gimme a number, buddy: ‘); a = x + 1; 则在操作界面中,自动调入输入的数字,输出加一的值。 fprintf(‘This concludes Lesson 3
’): 在command window里print除了
以外的文本;
指“Go to a new line”即换行 Edit check_out Within editor window: function check_out(n,price) total = n*price; fprintf(‘%d items at %.2f each
Total = $%5.2f
‘,n,price,total); Within command window: check_out(3,2.71) 3 items at 2.71 each Total = $ 8.13 解释: %: scape character; 直接指代后面的三个数值——n, price, total After each % is a format specifier: n&f — a conversion character (how the argument is to print the output) %d: print using scientific notation, except for whole numbers %.2f: f means print using fixed point notation instead of scientific notation (normal decimal notation); .2指小数点后保留两位
: to start a new line %5.2f: 5 means to print using at least 5 spaces, in case it only needs 4 spaces, blank spaces will be inserted before digits. 来源于C语言 如何print “%”? 只要在fprintf里输入连续两个%(%%)即可。 例: fprintf(‘12.5%% of 1234 equals %.3f
‘,0.125*1234) 则输出:12.5% of 1234 equals 154.250 如何print “\”? — “\\” fprintf(‘This is a backslash character: \\
‘) 则输出:This is a backslash character: \ 如何print “ ‘ “? — “ ‘’ ” fprintf(‘How about a single quote (”)?
‘) 则输出:How about a single quote (‘)? 如果有4个%,但是后面的数值只有3个,安全的输出法是输出到第4个%之前,后面的不输出 如果有4个%,但是后面的数值有5个,会重复输出一遍直到第2个(第二次)%之前 若输入fprintf(‘%4.1f
‘,[1 2 3 4 5 6]),则会每一行都print一个数字直到6个结束