how to stop exponential notation, both in output and in variable editor

Hi is there any way, preferably from code, to stop exponential notation.
So any figures I display, or look at in the variable editor, are always in the format 0.0000000234 etc never 2.34e-8
Thanks for any help,
Tom

3 件のコメント

Brando Miranda
Brando Miranda 2017 年 3 月 26 日
is it possible only turn scientific notation for a specific disp command? as in disp_no_sicentific(0.00001) but for everything else just the standard thing is on?
Walter Roberson
Walter Roberson 2018 年 6 月 6 日
No; if you have a need like that you should probably fprintf() the data
Walter Roberson
Walter Roberson 2019 年 12 月 17 日
Brando:
You could write a disp_no_scientific function that queried the existing format setting, and activated g format, and then returned to the previous format. However, as noted by Titus, format g does use scientific notation for sufficiently large or small values. There is no format setting for fixed point. If you need that then you should be using fprintf() or perhaps num2str() with an appropriate % format.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2011 年 12 月 7 日
編集済み: John Kelly 2014 年 6 月 4 日

10 投票

For variables that you disp(), command
format long
or
format long g

5 件のコメント

Titus Edelhofer
Titus Edelhofer 2011 年 12 月 7 日
Hi Walter,
but as far as I know the "g" option will still use exponential notation if you have many leading or trailing zeros, e.g. for the 2.34e-8 example of Tom...?
Titus
Andrew Reibold
Andrew Reibold 2014 年 8 月 22 日
'format long g' helped me solve a similar issue. Thanks for sharing
Brando Miranda
Brando Miranda 2017 年 3 月 26 日
is it possible only turn scientific notation for a specific disp command? as in disp_no_sicentific(0.00001) but for everything else just the standard thing is on?
Sam H
Sam H 2018 年 3 月 14 日
yes, you can use 'fprintf'
a=0.0001234;
fprintf('%.7f\n',a)
Walter Roberson
Walter Roberson 2019 年 12 月 17 日
Brando:
You could write a disp_no_scientific function that queried the existing format setting, and activated g format, and then returned to the previous format. However, as noted by Titus, format g does use scientific notation for sufficiently large or small values. There is no format setting for fixed point. If you need that then you should be using fprintf() or perhaps num2str() with an appropriate % format.

サインインしてコメントする。

その他の回答 (3 件)

Titus Edelhofer
Titus Edelhofer 2011 年 12 月 7 日

7 投票

Hi Tom,
as far as I know there is no way to force MATLAB to always use fixed notion. For generating output you can use fprintf with %f to use fixed notion.
Titus

3 件のコメント

Frank
Frank 2023 年 4 月 20 日
You just saved me from ripping my hair out.
Lars Abrahamsson
Lars Abrahamsson 2023 年 10 月 24 日
This answer is the most useful I think.
Shubhankar
Shubhankar 2024 年 7 月 26 日
simple and best answer

サインインしてコメントする。

Deepan J
Deepan J 2018 年 9 月 27 日

1 投票

format long g

4 件のコメント

Walter Roberson
Walter Roberson 2018 年 9 月 27 日
Yes, we said that 7 years ago...
bzibubab bzibubab
bzibubab bzibubab 2019 年 12 月 17 日
lol
Bharat Motilal
Bharat Motilal 2020 年 12 月 10 日
lmao
Theara Tha
Theara Tha 2021 年 9 月 26 日
haha

サインインしてコメントする。

SOREL
SOREL 2018 年 6 月 6 日
編集済み: Walter Roberson 2018 年 6 月 6 日

0 投票

Hi with the live editor I got a resul printed like this
A = 1.6050541506550198034773572941784e-40*d2
since 1.6e-40 is 0 is it possible to force the the A variable to be 0

4 件のコメント

Walter Roberson
Walter Roberson 2018 年 6 月 6 日
There are three ways to do that:
  1. use feval(symengine) or evalin(symengine) to invoke some MuPAD code to do the replacement for you; or
  2. if you know the structure of the expression, use children() to break it up into pieces, make whatever changes you want to the pieces, then put the pieces back together again. Unfortunately when you operate at the MATLAB level for this, you must already know what the appropriate operation is between the parts: there is no way to use children() or related functions to extract the name of the current operation so that you can put the expression back together again
  3. use children() to break up the expression into pieces and make a list of the values that you plan to replace. Once you have done that, use subs() on the expression to replace the numeric value with your new value
Example:
>> A = sym('1.6050541506550198034773572941784e-40'*d2)
A =
1.6050541506550198034773572941784e-40*d2
>> chA = children(A)
chA =
[ d2, 1.6050541506550198034773572941784e-40]
>> subs(A, chA(2), 12)
ans =
12*d2
here I replaced the numeric value with 12 to illustrate that it you can make arbitrary replacements.
SOREL
SOREL 2018 年 6 月 17 日
Thanks.
I got this result when I did Cos(pi/2) = 1.6050541506550198034773572941784e-40 but by replacing pi with the symbolic expression of sym(pi) I got the right result. Cos(sym(pi)/2) = 0
Walter Roberson
Walter Roberson 2019 年 12 月 17 日
Note: there are additional new and quite obscure possibilities since R2019a. They are difficult to locate; if you do not already know they exist you are unlikely to find reference to them, and even if you know the exist you need a fair bit of experience with symbolic toolbox programming to make use of them :(
FYI if you're doing numerical calculations involving trigonometric functions multiples of pi and want to avoid the round-off error caused by the pi function not returning the exact transcendental value of π, use the sinpi or cospi functions instead.
y = cos(pi/2)
y = 6.1232e-17
z = cospi(1/2)
z = 0

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDevelop Apps Using App Designer についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by