MATLAB 帮助中心
显示变量的值
disp(X)
disp(X) 显示变量 X 的值,而不打印变量名称。显示变量的另一种方法是键入它的名称,这种方法会在值前面显示一个前导“X =”。
X
X =
如果变量包含空数组,则会返回 disp,但不显示任何内容。
disp
示例
全部折叠
创建一个数字变量和一个文本变量。
A = [15 150]; S = 'Hello World.';
显示每个变量的值。
disp(A)
15 150
disp(S)
Hello World.
显示矩阵,并为列提供标签 Corn、Oats 和 Hay。
Corn
Oats
Hay
X = rand(5,3); disp(' Corn Oats Hay')
Corn Oats Hay
0.8147 0.0975 0.1576 0.9058 0.2785 0.9706 0.1270 0.5469 0.9572 0.9134 0.9575 0.4854 0.6324 0.9649 0.8003
通过包含 HTML 超链接代码作为 disp 的输入来显示网页链接。例如,显示指向 MathWorks® 网站的链接。
X = '<a href = "https://www.mathworks.com">MathWorks Web Site</a>'; disp(X)
MathWorks Web Site
以下介绍了可以在命令行窗口的同一行中显示多个变量值的三种方法。
使用 [] 运算符将多个字符向量串联在一起。使用 num2str 函数将任何数值转换为字符。使用 disp 显示结果。
[]
num2str
name = 'Alice'; age = 12; X = [name,' will be ',num2str(age),' this year.']; disp(X)
Alice will be 12 this year.
使用 sprintf 创建文本,然后通过 disp 显示它。
sprintf
name = 'Alice'; age = 12; X = sprintf('%s will be %d this year.',name,age); disp(X)
使用 fprintf 直接显示文本,无需创建变量。不过,要确保正常终止显示,必须在文本末尾处添加换行 (\n) 元字符。
fprintf
\n
name = 'Alice'; age = 12; fprintf('%s will be %d this year.\n',name,age);
输入数组。
要显示多个数组,您可以使用如在同一行上显示多个变量中所示的串联、sprintf 或 fprintf 函数。
全部展开
backgroundPool
ThreadPool
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
在 R2006a 之前推出
format | int2str | num2str | sprintf | fprintf | formattedDisplayText
format
int2str
formattedDisplayText
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处