MATLAB语言基础学习之disp函数

disp函数

Display value of variable

Syntax

disp(X)

Description

disp(X) displays the value of variable X without printing the variable name. Another way to display a variable is to type its name, which displays a leading “X =” before the value.

If a variable contains an empty array, disp returns without displaying anything.

Examples

Display Variable Values
Create a variable with numbers and another variable with text.
Display the value of each variable.

>> A = [15 150];
>> disp(A)
    15   150
>> S = 'Hello World.';
>> disp(S)
Hello World.

Display Matrix with Column Labels
Display a matrix and label the columns as Corn, Oats, and Hay.

>> disp('     Corn      Oats      Hay')
     Corn      Oats      Hay
>> X = rand(5,3);
>> disp(X)
    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

Display Hyperlink in Command Window
Display a link to a Web page by including HTML hyperlink code as input to disp. For example, display a link to the MathWorks® Web site.

>> X = '<a href = "https://www.mathworks.com">MathWorks Web Site</a>';
>> disp(X)

MathWorks Web Site

Display Multiple Variables on Same Line
Here are three ways to display multiple variable values on the same line in the Command Window.

Concatenate multiple character vectors together using the [] operator. Convert any numeric values to characters using the num2str function. Use disp to display the result.

>> name = 'Alice';  
>> age = 12;
>> X = [name,' will be ',num2str(age),' this year.'];
>> disp(X)
Alice will be 12 this year.

Use sprintf to create text, and then display it with disp.

>> name = 'Alice';
>> age = 12;
>> X = sprintf('%s will be %d this year.',name,age);
>> disp(X)
Alice will be 12 this year.

Use fprintf to directly display the text without creating a variable. However, to terminate the display properly, you must end the text with the newline (\n) metacharacter.

>> name = 'Alice';
>> age = 12;
>> fprintf('%s will be %d this year.\n',name,age);
Alice will be 12 this year.
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值