Thread Subject: How do I do this.....?

Subject: How do I do this.....?

From: s

Date: 4 Mar, 2003 18:59:08

Message: 1 of 6

The code:

=======================
disp 'The predicted pressure loss through the system is'

        DeltaPt = (DeltaPs + DeltaPf)
        disp 'N/m^2'

=====================

displays the answer:
======================================
The predicted pressure loss through the system is

DeltaPt =

  1.3044e+003

N/m^2

======================================
How can I code it to show like this:
The predicted pressure loss through the system is

DeltaPt = 1.3044e+003 N/m^2

======================================


SS.

Subject: How do I do this.....?

From: Matt Wolinsky

Date: 4 Mar, 2003 14:04:24

Message: 2 of 6

s wrote:
>
>
> The code:
>
> =======================
> disp 'The predicted pressure loss through the system is'
>
> DeltaPt = (DeltaPs + DeltaPf)
> disp 'N/m^2'
>
> =====================
>
> displays the answer:
> ======================================
> The predicted pressure loss through the system is
>
> DeltaPt =
>
> 1.3044e+003
>
> N/m^2
>
> ======================================
> How can I code it to show like this:
> The predicted pressure loss through the system is
>
> DeltaPt = 1.3044e+003 N/m^2
>
> ======================================
>
>
> SS.
>
>
>


I use disp with a single string for cases like this:


disp(['DeltaPt = ' num2str(DeltaPs + DeltaPf)
 'N/m^2']);


I think there are also C-style functions you could use.


Matt

Subject: How do I do this.....?

From: s

Date: 4 Mar, 2003 19:07:14

Message: 3 of 6

ta..

Subject: How do I do this.....?

From: Randy Poe

Date: 4 Mar, 2003 14:14:43

Message: 4 of 6

s wrote:
> The code:
>
> =======================
> disp 'The predicted pressure loss through the system is'
>
> DeltaPt = (DeltaPs + DeltaPf)
> disp 'N/m^2'
>
> =====================
>
> displays the answer:
> ======================================
> The predicted pressure loss through the system is
>
> DeltaPt =
>
> 1.3044e+003
>
> N/m^2
>
> ======================================
> How can I code it to show like this:
> The predicted pressure loss through the system is
>
> DeltaPt = 1.3044e+003 N/m^2
>
> ======================================

You get much more flexibility with fprintf() and
sprintf(), which allow the use of C-style format
strings. sprintf() constructs a string, which can be
output with disp. fprintf() allows you to print formatted
text to a file or to the screen.

The following code will work for you:

fprintf(1, 'The predicted pressure loss through the system is\n');
DeltaPt = (DeltaPs + DeltaPf);
fprintf(1, '\nDeltaPt = %11.4e N/m^2\n', DeltaPt)

Note:
  1. I use fprintf(1, ...) to print to screen.
  2. I have to insert a newline (\n) everywhere I want a
  line to end. The one before DeltaPt skips an extra line.
  3. I put a semicolon after the assignment of DeltaPt so it
  no longer exchoes the value. Instead, the fprintf() call
  handles anything going to the screen.

         - Randy

Subject: How do I do this.....?

From: s

Date: 4 Mar, 2003 21:33:00

Message: 5 of 6

fprintf(1, '\nDeltaPt = %11.4e N/m^2\n', DeltaPt)

what does the %11.4e do?

Subject: How do I do this.....?

From: Bronsing

Date: 5 Mar, 2003 13:03:10

Message: 6 of 6



s <webmaster@srsteel.co.uk replace webmaster with simon to reply.> wrote in
message news:b4362c$oeo$1$8302bc10@news.demon.co.uk...
> fprintf(1, '\nDeltaPt = %11.4e N/m^2\n', DeltaPt)
>
> what does the %11.4e do?
>
>

It denotes the C-style format for fprintf or sprintf to print. The format is
as follows:

     %11.4e

% is the start of a conversion specification
The number 11 denotes the minimum field width, in this case 11 characters
(but you could use 99 if you wanted to too);
.4 (with the point) denotes the precision, so (in this case) 4 decimal
places.
e is the conversion character, and the e means scientific notation (1e3 in
stead of 1,000)
Of course, the value of the argument (DeltaPt) will be put in the specified
format. Nifty feature :-)

There are many more conversion characters (like %d, %i, %o etc.). Look up in
a C book what each does and look in the matlab help what the differences are
between the conversions in C and those in Matlab.


hth

--

Robert Bronsing

Can't you see?
It all makes perfect sense,
expressed in dollars and cents, pounds, shillings and pence

(R. Waters)

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com