You are on page 1of 6

MATLAB Central - Text and variable in one line https://www.mathworks.com/matlabcentral/newsreader/view_thread/2...

Text and variable in one line Add to My Watch


List

MATLABText
Subject: Central Home
and variable in oneMy Newsreader
Reply to Page Post A New Message
this message What isSearch
Advanced a Watch
line Add author to My Watch List List?
From:
Help Ben View original format
Date: 23 Mar, 2009 20:40:20 Flag as spam
Tags for this
Message: 1 of 8
Thread
How do you display both a text and a numerical value for a variable in just one line, text,
one line. For example, here are my variable string, hi,
there is a small t...
x = 2;
Add a New Tag:
How do i display the result as

The value of x is 2. Add

I tried What are tags?


disp('The value of x is'); disp(x);
A tag is like a keyword
or category label
but the numerical value is not in line with the text. associated with each
Thanks thread. Tags make it
easier for you to find
threads of interest.

Anyone can tag a


thread. Tags are
Subject: Text and variable in one Reply to this message
public and visible to
line Add author to My Watch List everyone.
From: Matt Fig View original format
Date: 23 Mar, 2009 20:59:02 Flag as spam
Message: 2 of 8

fprintf('The value of x is %i.\n',x)

Subject: Text and variable in one Reply to this message


line Add author to My Watch List
From: Joe Sababa View original format
Date: 23 Mar, 2009 21:07:01 Flag as spam
Message: 3 of 8

"Ben " <benlawr2000@yahoo.com> wrote in message


<gq8s3k$n21$1@fred.mathworks.com>...
> How do you display both a text and a numerical value for a variable in just
one line. For example, here are my variable
>
> x = 2;

1 of 6 4/23/2017 7:14 PM
MATLAB Central - Text and variable in one line https://www.mathworks.com/matlabcentral/newsreader/view_thread/2...

>
> How do i display the result as
>
> The value of x is 2.
MATLAB Central Home My Newsreader Page Post A New Message Advanced Search
>
> I tried
Help
> disp('The value of x is'); disp(x);
>
> but the numerical value is not in line with the text.
> Thanks
Hi,
disp([The value of x is ' num2str(x)])

Joe

Subject: Text and variable in one Reply to this message


line Add author to My Watch List
From: Amal Elawad View original format
Date: 28 Mar, 2017 16:02:04 Flag as spam
Message: 4 of 8

"Joe Sababa" wrote in message <gq8tll$g69$1@fred.mathworks.com>...


> "Ben " <benlawr2000@yahoo.com> wrote in message
<gq8s3k$n21$1@fred.mathworks.com>...
> > How do you display both a text and a numerical value for a variable in
just one line. For example, here are my variable
>>
> > x = 2;
>>
> > How do i display the result as
>>
> > The value of x is 2.
>>
> > I tried
> > disp('The value of x is'); disp(x);
>>
> > but the numerical value is not in line with the text.
> > Thanks
> Hi,
> disp([The value of x is ' num2str(x)])
>
> Joe

Subject: Text and variable in one Reply to this message


line Add author to My Watch List
From: Amal Elawad View original format

2 of 6 4/23/2017 7:14 PM
MATLAB Central - Text and variable in one line https://www.mathworks.com/matlabcentral/newsreader/view_thread/2...

Date: 28 Mar, 2017 16:03:03 Flag as spam


Message: 5 of 8

"Joe Sababa"
MATLAB wrote in message
Central Home <gq8tll$g69$1@fred.mathworks.com>...
My Newsreader Page Post A New Message Advanced Search
> "Ben " <benlawr2000@yahoo.com> wrote in message
<gq8s3k$n21$1@fred.mathworks.com>...
Help
> > How do you display both a text and a numerical value for a variable in
just one line. For example, here are my variable
>>
> > x = 2;
>>
> > How do i display the result as
>>
> > The value of x is 2.
>>
> > I tried
> > disp('The value of x is'); disp(x);
>>
> > but the numerical value is not in line with the text.
> > Thanks
> Hi,
> disp([The value of x is ' num2str(x)])
>
> Joe

Subject: Text and variable in one Reply to this message


line Add author to My Watch List
From: Amal Elawad View original format
Date: 28 Mar, 2017 16:08:03 Flag as spam
Message: 6 of 8

"Joe Sababa" wrote in message <gq8tll$g69$1@fred.mathworks.com>...


> "Ben " <benlawr2000@yahoo.com> wrote in message
<gq8s3k$n21$1@fred.mathworks.com>...
> > How do you display both a text and a numerical value for a variable in
just one line. For example, here are my variable
>>
> > x = 2;
>>
> > How do i display the result as
>>
> > The value of x is 2.
>>
> > I tried
> > disp('The value of x is'); disp(x);
>>
> > but the numerical value is not in line with the text.
> > Thanks

3 of 6 4/23/2017 7:14 PM
MATLAB Central - Text and variable in one line https://www.mathworks.com/matlabcentral/newsreader/view_thread/2...

> Hi,
> disp([The value of x is ' num2str(x)])
>
> Joe
MATLAB Central Home My Newsreader Page Post A New Message Advanced Search
Hi,
A small typo though:
Help
disp(['The value of x is ', num2str(x)])

Subject: Text and variable in one Reply to this message


line Add author to My Watch List
From: Nasser M. Abbasi View original format
Date: 28 Mar, 2017 21:45:15 Flag as spam
Message: 7 of 8

On 3/28/2017 11:08 AM, Amal Elawad wrote:

> Hi,
> A small typo though:
>
> disp(['The value of x is ', num2str(x)])
>

better I think to use sprintf. More standard and can


have more control on formating.

>> x=pi; sprintf('The value of x is %f\n',x)

The value of x is 3.141593

Subject: Text and variable in one Reply to this message


line Add author to My Watch List
From: dpb View original format
Date: 29 Mar, 2017 02:43:02 Flag as spam
Message: 8 of 8

On 03/28/2017 4:45 PM, Nasser M. Abbasi wrote:


...

> better I think to use sprintf. More standard and can


> have more control on formating.
>
>>> x=pi; sprintf('The value of x is %f\n',x)
>

4 of 6 4/23/2017 7:14 PM
MATLAB Central - Text and variable in one line https://www.mathworks.com/matlabcentral/newsreader/view_thread/2...

>
> The value of x is 3.141593

"Anything you can do, I can do, too!"


MATLAB Central Home My Newsreader Page Post A New Message Advanced Search
Sincerely,
NUM2STR()
Help
>> num2str(pi,'The value of x is %f')
ans =
The value of x is 3.141593
>>

:)

The advantage is NUM2STR automatically builds arrays that SPRINTF()


can't...

>> x=[1:3].'*pi;
>> num2str(x,'The value of x is %f')
ans =
The value of x is 3.141593
The value of x is 6.283185
The value of x is 9.424778
>> whos ans
Name Size Bytes Class Attributes
ans 3x26 156 char

>> sprintf('The value of x is %f\n',x)


ans =
The value of x is 3.141593
The value of x is 6.283185
The value of x is 9.424778

>> whos ans


Name Size Bytes Class Attributes
ans 1x81 162 char

>>

While the second case superficially _looks_ the same on the screen,
that's only because of the embedded \n; it's still just one long string
in actuality.

OTOH, the NUM2STR() result is a 3xN character array so the three


elements can be addressed individually. This is specifically of much
use in writing values for ticks or specific graphs or legends or
labeling bars in histograms, etc., etc., etc., where the vectorized
return is invaluable.

For a single string it really makes no difference; as you say SPRINTF()


serves just as well but there's a reason NUM2STR() exists.

5 of 6 4/23/2017 7:14 PM
MATLAB Central - Text and variable in one line https://www.mathworks.com/matlabcentral/newsreader/view_thread/2...

--

MATLAB Central Home My Newsreader Page Post A New Message Advanced Search
Feed for this Thread
Help

1994-2017 The MathWorks, Inc.

6 of 6 4/23/2017 7:14 PM

You might also like