Why do different number types display in different positions?
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
dpb
el 6 de Mayo de 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
el 7 de Mayo de 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.
Respuestas (1)
Yoga
el 25 de Jul. de 2023
Editada: Yoga
el 25 de Jul. de 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
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!