Why do different number types display in different positions?
Show older comments
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.
Les Beckham
on 5 May 2023
You edited away the inconsistent spacing that was the basis of @Richard Reilly's main question. However, your edit did address the "side question".
dpb
on 5 May 2023
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...
Richard Reilly
on 6 May 2023
dpb
on 6 May 2023
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...
randi(256,5,23) % but why do you want to restrict me to only seeing 4 columns here?
randi([1e6,2e6],5,23) % it even adjusts depending on the magnitude
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,
dpb
on 6 May 2023
@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....
Richard Reilly
on 6 May 2023
format longg, format compact
x=pi
x=3
format longe, format compact
x=pi
x=3
If it's only the same starting location you want, use longe instead of longg
Rik
on 7 May 2023
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.
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!