Azzera filtri
Azzera filtri

Why do different number types display in different positions?

13 visualizzazioni (ultimi 30 giorni)
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 Commenti
dpb
dpb il 6 Mag 2023
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
Rik
Rik il 7 Mag 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.

Accedi per commentare.

Risposte (1)

Yoga
Yoga il 25 Lug 2023
Modificato: Yoga il 25 Lug 2023
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

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by