NLe*_*Led 7 matlab printf command-window
当我使用sprintf时,结果显示如下:
sprintf('number=%d %d %d',a,b,c)
sprintf('or %d',h)
ans =
number= 5 4 2
ans =
or 2
Run Code Online (Sandbox Code Playgroud)
如何在不ans =妨碍结果的情况下显示结果?
选项1:disp(['A string: ' s ' and a number: ' num2str(x)])
选项2:disp(sprintf('A string: %s and a number %d', s, x))
选项3:fprintf('A string: %s and a number %d\n', s, x)
引用http://www.mathworks.com/help/matlab/ref/disp.html(在同一行显示多个变量)
在命令窗口中,有三种方法可以在同一行上显示多个变量.
(1)使用[]运算符将多个字符串连接在一起.使用num2str函数将任何数值转换为字符.然后,使用disp显示字符串.
name = 'Alice';
age = 12;
X = [name,' will be ',num2str(age),' this year.'];
disp(X)
Run Code Online (Sandbox Code Playgroud)
Alice will be 12 this year.
(2)您也可以使用sprintf创建一个字符串.使用分号终止sprintf命令以防止显示"X =".然后,使用disp显示字符串.
name = 'Alice';
age = 12;
X = sprintf('%s will be %d this year.',name,age);
disp(X)
Run Code Online (Sandbox Code Playgroud)
Alice will be 12 this year.
(3)或者,使用fprintf创建和显示字符串.与sprintf函数不同,fprintf不显示"X ="文本.但是,您需要使用换行符(\n)元字符结束字符串以正确终止其显示.
name = 'Alice';
age = 12;
X = fprintf('%s will be %d this year.\n',name,age);
Run Code Online (Sandbox Code Playgroud)
Alice will be 12 this year.