How can I make the value in the workspace show more than 4 digits?

52 visualizaciones (últimos 30 días)
Suleyman Aliyev
Suleyman Aliyev el 10 de Nov. de 2024
Movida: John D'Errico el 30 de Nov. de 2024 a las 13:19
Hi! Can someone explain to me how I can make the value in workspace show more than 4 values after the decimal point? I read that it is possible to enter format long, but I did not succeed, and I do not want to pollute the code, I could not find information in the settings, please help!
  2 comentarios
Stephen23
Stephen23 el 12 de Nov. de 2024
I do not recall that there is a simple way to change the workspace viewer format.
Suleyman Aliyev
Suleyman Aliyev el 30 de Nov. de 2024 a las 12:12
Movida: John D'Errico el 30 de Nov. de 2024 a las 13:19
Thank you all for your help!

Iniciar sesión para comentar.

Respuestas (3)

Walter Roberson
Walter Roberson el 10 de Nov. de 2024
The other answers are not exactly wrong... but format does not control the display of values in the Workspace browser. Control of the display of values in the Workspace browser is through Preferences -> Variables -> MATLAB Variables Preferences -> Format -> Default array format:
You might change the drop-down there to "long g"
  1 comentario
Suleyman Aliyev
Suleyman Aliyev el 12 de Nov. de 2024
I changed it, but nothing has changed, it also shows 4 digits after the decimal point

Iniciar sesión para comentar.


Sathvik
Sathvik el 10 de Nov. de 2024
Hi
MATLAB uses a 5-digit short format to display numbers by default, as described here:
The "format" function that you tried to use with "format long" would help displaying more than 5 digits. If you are looking to change the way they appear, you can refer to the above documentation. However, this only affects how they appear, not how MATLAB saves the value. MATLAB uses 16 digits of precision by default:
For higher precision, you can use the "vpa" function in the Symbolic Math Toolbox as mentioned in the documentation above.

Umar
Umar el 10 de Nov. de 2024

Hi @Suleyman Aliyev ,

The format command in MATLAB allows you to control the display format of numerical values. To display more than four decimal places, you can use the format long command. This command changes the display format to show up to 15 digits for double-precision numbers.

https://www.mathworks.com/help/matlab/ref/format.html

Here’s how to use it:

x = 1/3; % Example calculation
disp(x); % This will display the value with more decimal places

If you want to avoid repeatedly entering format long in your scripts, you can set it in your MATLAB startup file. This way, every time you start MATLAB, it will automatically use the long format. To do this, follow these steps:

Locate your startup.m file. If it does not exist, you can create one in your MATLAB user directory.

Open the startup.m file and add the following line:

        format long

Save the file and restart MATLAB. Now, every time you open MATLAB, it will display numbers in long format by default.

If you want to display specific values with a custom number of decimal places without changing the global format, you can use the fprintf function. This function allows you to specify the number of decimal places for each output. Here’s an example:

   value = pi; % Example value
   fprintf('Value of pi to 10 decimal places: %.10f\n', value);

If you have any further questions or need additional assistance, feel free to ask!

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by