First value of an array does not display with a decimal

55 views (last 30 days)
I am trying to print an array which display the values of x y and z on every change of x and z, but the first value os always formated a different way if z is an integer. Here is the awnser I get:
And here is my code:
zp=input('Įveskite pradinę z vertę: ');
zg=input('Įveskite galutinę z vertę: ');
hz=input('Įveskite z vertės pokytį: ');
xp=4;
hx=0.3;
x=xp;
disp(' x y z');
for z=zp:hz:zg
if z>=x
y=sin(x).*cos(x);
else
y=log10(xp).*log(z);
end
disp([x y z]);
x=x+hx;
end

Accepted Answer

Steven Lord
Steven Lord on 22 Nov 2025 at 16:37
The disp function doesn't let you control the format with which the values are displayed. Instead, use either fprintf or (what I would recommend) create a table array and display it.
data = [1 2 3; magic(3)/2] % sample data
data = 4×3
1.0000 2.0000 3.0000 4.0000 0.5000 3.0000 1.5000 2.5000 3.5000 2.0000 4.5000 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
T = array2table(data, VariableNames = ["x", "y", "z"])
T = 4×3 table
x y z ___ ___ ___ 1 2 3 4 0.5 3 1.5 2.5 3.5 2 4.5 1
  2 Comments
dpb
dpb on 22 Nov 2025 at 16:51
Edited: dpb on 22 Nov 2025 at 16:57
But the table has the same issue as the command window in that it uses the present format setting and thus the formatting isn't uniform. This is always irritating in use, but the table isn't designed for pretty output but for analysis, granted.
data = [1 2 3; magic(3)/2]; % sample data
format bank % set a uniform number of decimal places--unfortunately, only two
T = array2table(data, VariableNames = ["x", "y", "z"])
T = 4×3 table
x y z ____ ____ ____ 1.00 2.00 3.00 4.00 0.50 3.00 1.50 2.50 3.50 2.00 4.50 1.00
format shortE
T
T = 4×3 table
x y z __________ __________ __________ 1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e-01 3.0000e+00 1.5000e+00 2.5000e+00 3.5000e+00 2.0000e+00 4.5000e+00 1.0000e+00
gives one the desired precision at the cost of the unwanted exponent.
I recall asking for the facility to specify a format string as an option for format clear back when first began using MATLAB with Ver 3.1 but it has never been adopted.
Ernestas
Ernestas on 22 Nov 2025 at 17:02
Thank you for your awnser, this is quite useful, but I have found another way of making it uniform. Since my code calculates the values of y, it is the variable which is responsible for the formating (specifically when y=0).
I have found a way of overcoming this by just incrementing y by a miniscule amount so it would display as 0.000

Sign in to comment.

More Answers (2)

Torsten
Torsten on 22 Nov 2025 at 16:38
If you like it more, you can use
sprintf('%f %f %f',[x, y, z])
instead of
disp([x y z]);
  1 Comment
dpb
dpb on 22 Nov 2025 at 17:04
Edited: dpb on 22 Nov 2025 at 17:06
Without the specified precision, the above will produce six digits past the decimal so OP will have to adjust the heading to keep the fields aligned...

Sign in to comment.


dpb
dpb on 22 Nov 2025 at 16:32
Edited: dpb on 23 Nov 2025 at 15:06
The default format for MATLAB display in the command window is '%g" (OK, so it is complicated thing that renders basically as, not precisely) which has the characteristic that integer-valued doubles are printed as integers, not floating point. This has a little advantage in normal use in that one can tell if a value is exactly an integer or has some rounding error that is not shown if the displayed result is "0.0" instead of just "0".
But, to control the actual precision, one must set the format explicitly or use a different format in the command window; there are only a relatively few of those available, however, and a specific number of digits in '%f" format isn't one of them(*).
zp=input('Įveskite pradinę z vertę: ');
zg=input('Įveskite galutinę z vertę: ');
hz=input('Įveskite z vertės pokytį: ');
xp=4;
hx=0.3;
x=xp;
disp(' x y z');
fmt=[repmat('%8.4f',1,3) newline]; % build the formatting string
for z=zp:hz:zg
if z>=x
y=sin(x).*cos(x);
else
y=log10(xp).*log(z);
end
fprintf(fmt,x,y,z); % print using the format
x=x+hx;
end
(*) The one exception to the general statement is format bank that sets two decimal places.
  2 Comments
Walter Roberson
Walter Roberson on 22 Nov 2025 at 17:43
Default
default restores the default display format, which is short for numeric format and loose for line spacing. (since R2021a)
So the default is not %g .
format short examines the set of values to be displayed, and if they are all integer-valued and have absolute value at most 999999999, then it displays them in integer format. If even one of them is non-integer then a format with decimal points is used.
format short
9e8
ans = 900000000
-9e8
ans = -900000000
9e8+0.5
ans = 9.0000e+08
[9e8 9e8]
ans = 1×2
900000000 900000000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[9e8 9e8]+[0 0.5]
ans = 1×2
1.0e+08 * 9.0000 9.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  • all integers in range: displays as integers
  • single non-integral number: displays as single number including exponent
  • multiple numbers with at least one non-integral: displays leading scale factor, then below that displays scaled numbers with no exponent and 4 decimal places
Walter Roberson
Walter Roberson on 23 Nov 2025 at 18:32
I see your edits, but it is still true that the default display format is not %g but rather "format short"

Sign in to comment.

Products


Release

R2025b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!