Why do different number types display in different positions?

Working on a project for Astro, and answers display differently. (I've always had this happen though.) Is there any way to tell Matlab to line them up at the same point so the output is easier to read?
Whole numbers display under the variable text (5-space tab), while decimal answers display further tabbed out (10-space tab). My output looks like this:
SLat1 =
32.3960705606065
SDEG =
32
SMIN =
23
SSEC =
45.8540181832331
My code is basic m-script, starts with formats longg and compact. Variables are displayed by simply omitting the semicolon at the end of the line it's generated on, as it's less coding than fprintfing everything, and I have to transcribe the answers to paper anyway. (I am aware I can 'format' the space distances using fprintf, but that doesn't properly fix the actual issue)
It does the same thing (only worse) when I just type into the command line (decimal is 22-space tab):
>> 5+3
ans =
8
>> 5.5+6
ans =
11.5
Side question, is there no method to change line spacing in this text box? Copying from matlab adds more space than default, but I don't see any options to squeeze the lines together?
Thanks,
Richard

10 Comments

"... but that doesn't properly fix the actual issue"
I guess I'm at a loss as to what the actual issue is, then.
It reminds me of the complaint heard in the Fortran newsgroups about wanting to use "*" free-form formatting but then expecting it to follow some preconceived notion of what not "free-form" means.
You have some control over the output formatting MATLAB uses in the format function, but its pretty limited number of options. Choosing longg for that form, however, ensures the output will vary greatly between integer and noninteger values given the underlying definition of %g to output a given precision result in a minimal number of characters. longe would create consistency, but perhaps not be as pretty to the human.
In short, however, the same answer holds in MATLAB as in Fortran -- "if it is important how the output looks, then specify and format it to produce that result".
You could probably save some coding effort by writing a display function and pass it the result instead of writing explicit formatting strings every time.
You edited away the inconsistent spacing that was the basis of @Richard Reilly's main question. However, your edit did address the "side question".
Ewwww....I didn't actually realize it had done that....I just presumed the online would still stay...ok here's a run at local command line that uses OP's format setting --
>> format longg;format compact; SLat1 =32.3960705606065
SDEG =32
SMIN =23
SSEC =45.8540181832331
SLat1 =
32.3960705606065
SDEG =
32
SMIN =
23
SSEC =
45.8540181832331
>>
I think it's really immaterial what the actual spacing itself is; the issue is the false expectation of being able to control "free" formatting...
It's not a big deal, it just affects readabilty when copying answers over from Matlab.
I don't understand why matlab would output answers at different locations depending on if the answer is a whole number or a decimal. It's not a matter of controlling per se, it's a matter of matlab doesn't line it up, so can I control it to do so?
Shortg/longg was just to explain what I was using, they don't actually affect the question I'm asking
Nope, no can do just from the default output.
You can control output as you've already indicated you know by using one of the formatted output functions; the new(ish) compose might be something to look at; it's vectorized in the more useful fashion than are fprintf and friends; it will handle each array sequentially by position instead of dumping one, then the next.
If you have a specific thing you're doing with output to the command window about copying and pasting it somewhere else more than just once a week, I'd say it would be worth building a little outputting function that would write that directly to a file or the clipboard formatted as you would like.
"I don't understand why matlab would output answers at different locations depending on if the answer is a whole number or a decimal."
To maximize what can be displayed in matrices (after all, this tool's name is "MATrix LABoratory"). Compare:
format longg; format compact
rand(5,23) % I can see 4 columns on this forum, which seems reasonable...
ans = 5×23
0.349497078987956 0.520162787471063 0.216311993470789 0.00395188164757609 0.856429663468165 0.0425586292346518 0.543891362538376 0.0134526957774276 0.375948426458186 0.621940166870231 0.59502155951945 0.445541167840553 0.268780186606545 0.285967523998255 0.168186781245476 0.547753130104293 0.788471773120448 0.0631131190978249 0.83311939514401 0.567783517916315 0.673615530919089 0.74425619769326 0.668369490303301 0.690616071383127 0.130122225515693 0.0622419188789974 0.839006774043873 0.84083159557231 0.543128149408544 0.751976910767442 0.0244116482596942 0.0741626846338155 0.388454006238588 0.995707082235482 0.853071012825067 0.026493293306591 0.762111879031979 0.342527208818007 0.383635163891285 0.912049913435893 0.191490608785793 0.207261744853131 0.428064392767835 0.950533859470425 0.846221241885404 0.571313711703208 0.232291573341011 0.814097127110164 0.494405476799685 0.937537586020591 0.725512539256731 0.224056187515876 0.578653895752999 0.887484197688756 0.257791221671897 0.416556943540545 0.448253298963671 0.0503531096848064 0.542861051108789 0.812549890373686 0.705952195312901 0.658991028084326 0.658617654487094 0.372401412621124 0.304727337662858 0.207820486897156 0.211928282299805 0.901967994840638 0.514572682095489 0.553923492295034 0.353345412382654 0.279642373739878 0.922138168668889 0.327290263988895 0.177258625471207 0.754336037098472 0.996971110968171 0.21330225593256 0.682290076290669 0.681985434820621 0.712739066869571 0.0604886992826128 0.493214801392953 0.478786854120402 0.110861402538624 0.113880473141661 0.179314291250215 0.733366969431609 0.222266393677978 0.589185620392178 0.748774952295356 0.803999398166694 0.85189944821511 0.631068678719725 0.0966678588367563 0.398365385225321 0.377530075650947 0.420746623292896 0.293353613017049 0.573581653473506 0.568827479351651 0.602914386506062 0.767891137550705 0.80694025439122 0.0728012455137029 0.274745802657556 0.251546089120848 0.0549171937916049 0.56591241358087 0.826376728937011 0.325127261657112 0.282396231435361 0.467801705123007 0.200833542150472 0.161638012745643
randi(256,5,23) % but why do you want to restrict me to only seeing 4 columns here?
ans = 5×23
210 32 5 21 124 41 63 123 173 218 247 3 81 194 166 185 92 18 58 180 248 194 252 88 27 71 71 138 101 198 109 110 225 71 218 232 64 136 186 223 226 41 214 72 70 60 179 236 236 144 60 98 208 218 95 195 15 11 103 87 200 174 66 107 193 93 45 135 3 33 161 191 125 20 255 141 75 8 67 119 150 207 78 59 32 153 87 58 207 179 113 224 221 204 31 93 58 168 102 155 3 66 44 1 213 195 6 6 148 168 131 205 36 220 226
randi([1e6,2e6],5,23) % it even adjusts depending on the magnitude
ans = 5×23
1309688 1520731 1759006 1880740 1227258 1566287 1134506 1213893 1781440 1949500 1538486 1274864 1602915 1043852 1941154 1128686 1348718 1090213 1294747 1643175 1798715 1354482 1181727 1834317 1074116 1661051 1810126 1504908 1530720 1388536 1171976 1787104 1873159 1364011 1412045 1437234 1045032 1681670 1319873 1514264 1378447 1902619 1854408 1681565 1339298 1397204 1912685 1747033 1982925 1057681 1970234 1388980 1185373 1367305 1002803 1762542 1772419 1829498 1906232 1563960 1273521 1950231 1036076 1399097 1221114 1657171 1844779 1291963 1706133 1601636 1430315 1452951 1049612 1461868 1763910 1505475 1933723 1499223 1395238 1590530 1696327 1619563 1795770 1897947 1759870 1049429 1098313 1972068 1804177 1329371 1351673 1096829 1985521 1862111 1268766 1382273 1414825 1438328 1295541 1716726 1993601 1987714 1894347 1233479 1293280 1384632 1880707 1501881 1114124 1896506 1185838 1941791 1972921 1808975 1258939
MATLAB makes a reasonable effort to display as much information as it can while keeping the values aligned and neat, which also seems reasonable. Sure, it could just fill the screen without any whitespace or alignment: would that be useful? (hint: no). Or it could align everything consistently in to the maximum selected precision, forcing me to scroll to see the 5th column of any matrix :(
Numeric formatting is a case of you can't make everyone happy all the time.
As dpb wrote, if you want complete control of how numbers look like, that is exactly what FPRINTF is for,
@Stephen23, that's good addition to note the difference for arrays and not just the single element...I was focusing on the difference between integer and non by the definition of the '%g' format; it's also interesting that MATLAB does the same thing with long/shorte of not displaying the decimals but it does then align the columns that longg doesn't....
Matrix stuff hasn't bothered me yet, if it goes too far it defaults to printing on a following line. And that formatting makes sense, as MATLAB formats each column width to the maximum display for any given entry. That's expected.
My question was solely on the start point location for outputting a single integer vs decimal-point. Shortg, longg, whatever the default is, it's just weird that the start point for a single answer is at different locations for different types of variable. So trying to read a one-by-one-by-one list of outputs means looking at different locations each line and gets awkward. There are no other numbers changing the column width making a different number have a lot of weird whitespace.
Also, I did fix the spacing issue that the code boxen removed, so the original post should now show the issue again.
format longg, format compact
x=pi
x = 3.14159265358979
x=3
x = 3
format longe, format compact
x=pi
x = 3.141592653589793e+00
x=3
x = 3
If it's only the same starting location you want, use longe instead of longg
I believe I have seen some trickery from Yair regarding the formatting, but since neither dpb nor Stephen mentioned it, I suspect that was limited to the >> marking and colors. I will have a longer look later, but you might have a go at googling this yourself as well.

Sign in to comment.

Answers (1)

I understand that you have an issue with the different line spacing schemes for whole numbers and decimal numbers.
It is not completely possible to do formatting in MATLAB command prompt even by using 'format longe, format compact' or other formatting methods. It is possible to display aligned output without using 'fprintf' explicitly. You can achieve this by leveraging the 'disp' function along with the default display settings.
For example:
% Set the desired format
format longg
% Your calculations here
SLat1 = 32.3960705606065;
SDEG = 32;
SMIN = 23;
SSEC = 45.8540181832331;
% Display the results with default alignment using disp
disp('SLat1 =');
disp(SLat1);
disp('SDEG =');
disp(SDEG);
disp('SMIN =');
disp(SMIN);
disp('SSEC =');
disp(SSEC);
The above code will give the result:
SLat1 =
32.3960705606065
SDEG =
32
SMIN =
23
SSEC =
45.8540181832331
MATLAB automatically aligns the output, but notably for integers, it might not generate the precise spacing and alignment you want. If accurate alignment is essential, using 'fprintf' is advised since it gives you better control over the output's formatting and alignment.
Having sad that, you can use the 'fprintf' function to format the output and align it. By doing this, you can make sure that the decimal numbers are correctly aligned.
Here is an example of how you can change your code to accomplish this:
% Set the desired format
format longg
% Your calculations here
SLat1 = 32.3960705606065;
SDEG = 32;
SMIN = 23;
SSEC = 45.8540181832331;
% Display the results with proper alignment using fprintf
fprintf('SLat1 = %.13f\n', SLat1);
fprintf('SDEG = %2d\n', SDEG);
fprintf('SMIN = %2d\n', SMIN);
fprintf('SSEC = %.13f\n', SSEC);
The above code will give the result:
SLat1 = 32.3960705606065
SDEG = 32
SMIN = 23
SSEC = 45.8540181832331

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 5 May 2023

Edited:

on 25 Jul 2023

Community Treasure Hunt

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

Start Hunting!