Thread View: comp.soft-sys.matlab
7 messages
7 total messages
Started by Mark Nathan Broadie
Sat, 23 Jan 1993 15:27
DISP1: disp1.m - display text and numbers
Author: Mark Nathan Broadie
Date: Sat, 23 Jan 1993 15:27
Date: Sat, 23 Jan 1993 15:27
43 lines
1608 bytes
1608 bytes
Trying to continue the trend, here is an m-file for display
text and numbers to the screen on a single line. I had m-files
filled with statements like
disp('x measures blah blah, its value is'); disp(x);
which (a) uses 2 display statements and (b) does not appear
on a single line on the screen. The m-file disp1.m allows the
statement
disp1('x measures blah blah, its value is ', x);
Keep these submissions coming!
-mark broadie
------ cut here -------
function disp1(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10);
% DISP1
% displays up to ten arguments on the screen
% e.g., x = 2.3;
% disp1('x = ', x, ', 2*x = ', 2*x);
% will print 'x = 2.3, 2*x = 4.6' on the screen
% Mark Broadie 1-20-93
% Copyright (c) 1993.
s = [];
for i = 1 : nargin;
x = eval(['s',int2str(i)]);
% num2str.m code follows with '%.6g' replacing '%.4g'
if isstr(x)
t = x;
else
t = sprintf('%.6g',x);
end
s = [s t];
end;
disp(s);
% end of disp1.m
Re: DISP1: disp1.m - display text and numbers
Author: rich
Date: Sat, 23 Jan 1993 23:32
Date: Sat, 23 Jan 1993 23:32
22 lines
1300 bytes
1300 bytes
In article <1993Jan23.152729.26892@news.columbia.edu> mnb2@cunixf.cc.columbia.edu (Mark Broadie) writes:
>Trying to continue the trend, here is an m-file for display
>text and numbers to the screen on a single line. I had m-files
>filled with statements like
> disp('x measures blah blah, its value is'); disp(x);
>which (a) uses 2 display statements and (b) does not appear
>on a single line on the screen. The m-file disp1.m allows the
>statement
> disp1('x measures blah blah, its value is ', x);
>
> [script deleted]
You don't need an m-file for that - just use:
disp(['x measures blah blah, its value is ' num2str(x)]);
--
Rich Pawlowicz ----------------- INTERNET: rich@boreas.whoi.edu
Woods Hole Oceanographic Institution
"Home of the 99 cent squid burger (cheese extra)"
--------------------------------------------------------------
Re: DISP1: disp1.m - display text and numbers
Author: Douglas Gray Stephens
Date: Mon, 25 Jan 1993 09:37
Date: Mon, 25 Jan 1993 09:37
31 lines
1877 bytes
1877 bytes
In article <1993Jan23.233224.11781@netnews.whoi.edu> rich@boreas.whoi.edu writes:
>Trying to continue the trend, here is an m-file for display
>text and numbers to the screen on a single line. I had m-files
>filled with statements like
> disp('x measures blah blah, its value is'); disp(x);
>which (a) uses 2 display statements and (b) does not appear
>on a single line on the screen. The m-file disp1.m allows the
>statement
> disp1('x measures blah blah, its value is ', x);
>
> [script deleted]
You don't need an m-file for that - just use:
disp(['x measures blah blah, its value is ' num2str(x)]);
If you want more control over the format of the numbers, you should
use the fprintf command, e.g.
fprintf('x measures blah blah, its value is %.4g\n',x)
(you also have more explicit control over the line breaks).
--
----------------------------------------------------------------------------
Dr. Douglas GRAY STEPHENS e-mail : gray@scr.slb.com
Schlumberger Cambridge Research Tel/fax: 0223-325303 / 0223-311830
P.O.Box 153, Cambridge CB3 0HG, England (International prefix for UK: 44)
============================================================================
Re: DISP1: disp1.m - display text and numbers
Author: Jeffrey A. Beck
Date: Mon, 25 Jan 1993 10:36
Date: Mon, 25 Jan 1993 10:36
28 lines
1843 bytes
1843 bytes
In article <1993Jan23.152729.26892@news.columbia.edu> mnb2@cunixf.cc.columbia.edu (Mark Broadie) writes:
>Trying to continue the trend, here is an m-file for display
>text and numbers to the screen on a single line. I had m-files
>filled with statements like
> disp('x measures blah blah, its value is'); disp(x);
>which (a) uses 2 display statements and (b) does not appear
>on a single line on the screen. The m-file disp1.m allows the
>statement
> disp1('x measures blah blah, its value is ', x);
>
>Keep these submissions coming!
>
>-mark broadie
Mark,
Try: disp(sprintf('x measures blah blah, it's value is %g',x))
It works on the PC version 3.5 I'm using.
Jeff
------------------------------------------------------------------------
JEFFREY A. BECK, Capt, USAF | ___/ __ __/ __ __/
Doctoral Student / | / / /
Dept of Aeronautics and Astronautics __ | __/ / /
Air Force Institute of Technology __/ __| __/ ______/ __/
------------------------------------------------------------------------
INTERNET: jbeck@afit.af.mil COMPUSERVE: 71371,260
------------------------------------------------------------------------
Re: DISP1: disp1.m - display text and numbers
Author: Mark Nathan Broadie
Date: Mon, 25 Jan 1993 14:28
Date: Mon, 25 Jan 1993 14:28
22 lines
1534 bytes
1534 bytes
Thanks for the comments about disp1.m. I realize you don't need
an m-file for this, but it's easier. disp1.m takes up to 10
arguments, with strings or numbers in any order. fprintf and
sprintf are limited to 3 arguments. disp1 is slightly simpler
and shorter than using num2str directly:
disp1('x = ', x, ' y = ', y)
vs.
disp(['x = ' num2str(x) ' y = ' num2str(y)])
In general, I find that display information 'nicely' is not easy
with MATLAB. For example, does anyone have a *simple* way to
display a small table with row and column headings. By simple,
I mean not having to count spaces. For example, an m-file
to display
Col1 head Col2 heading Col3 head
Row1 label 0.0 1.0 -3.4
Row2 label here 0.0 1.0 -3.4
Row3 label 0.0 1.0 -3.4
-mark broadie
Re: DISP1: disp1.m - display text and numbers
Author: ellert
Date: Mon, 25 Jan 1993 14:39
Date: Mon, 25 Jan 1993 14:39
18 lines
1277 bytes
1277 bytes
In article <GRAY.93Jan25093751@snow.scr.slb.com>, gray@snow.scr.slb.com (Douglas Gray Stephens) writes:
>If you want more control over the format of the numbers, you should
>use the fprintf command, e.g.
>
> fprintf('x measures blah blah, its value is %.4g\n',x)
>
>(you also have more explicit control over the line breaks).
>
A word of warning on the fprintf function -- under VMS 5.5-2, matlab versions
3.5f and 3.5g, I have had the ever so thrilling symbolic stack dump as a
result of placing fprintf calls inside loops. These calls have been to
to the display, rather than to files, and have had the legal number of
arguments.
Ed
ellert@nu1.uh.cwru.edu
Re: DISP1: disp1.m - display text and numbers
Author: rich
Date: Mon, 25 Jan 1993 23:42
Date: Mon, 25 Jan 1993 23:42
47 lines
2397 bytes
2397 bytes
In article <1993Jan25.142824.5268@news.columbia.edu> mnb2@cunixf.cc.columbia.edu (Mark Broadie) writes:
>Thanks for the comments about disp1.m. I realize you don't need
>an m-file for this, but it's easier. disp1.m takes up to 10
>arguments, with strings or numbers in any order. fprintf and
>sprintf are limited to 3 arguments.
ummm...no they aren't. For example:
>>fprintf('x = %3.1f, x2 = %3.1f, x3= %7.3f\n',x1,x2,x3);
x = 123.0, x2 = 321.0, x3= 3.000
>>
The ?printf functions have exactly the same format, strengths, and
weaknesses as the C ?printf commands, a feature that I'm sure implies
something deep about the way Matlab implemented this!
>
>In general, I find that display information 'nicely' is not easy
>with MATLAB. For example, does anyone have a *simple* way to
>display a small table with row and column headings. By simple,
>I mean not having to count spaces. For example, an m-file
>to display
>
> Col1 head Col2 heading Col3 head
>Row1 label 0.0 1.0 -3.4
>Row2 label here 0.0 1.0 -3.4
>Row3 label 0.0 1.0 -3.4
>
>
>>A=[0 1 -3.4;0 1 -3.4;0 1 -3.4];
>>fprintf('\t\tCol1 head\tCol2 head\tCol3 head\n');
>>fprintf('Row 1 label\t%6.1f\t\t%6.1f\t\t%6.1f\n',A(1,:));
>>fprintf('Row 2 label\t%6.1f\t\t%6.1f\t\t%6.1f\n',A(2,:));
...
will give you the correct look. Again, it's the same as C. By judicious
use of the tab (\t) and newline (\n) in some kind of print loop you can
easily write a .m file to output matrices of arbitrary size and labelling.
--
Rich Pawlowicz ----------------- INTERNET: rich@boreas.whoi.edu
Woods Hole Oceanographic Institution
"Home of the 99 cent squid burger (cheese extra)"
--------------------------------------------------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads