Problems accepting integer values with power and algebraic symbols using inputdlg

1 次查看(过去 30 天)
Hey guys, I'm trying to pass an integer values through to an inptdlg GUI. That said, this input may be a big number (using the power notation and multiplication sign) therefore, the input may consist of multiplication and power signs etc., that are (im assuming) defined as strings or char? The problem arises when trying to convert the input from the user denoted as double_ans back into an integer value, i recieve a NaN as shown below in image 2. Is there any work arounds to this problem where I don't have to seperate the integer value from strings/symbols/chars?
To re-emphasise, when inputing a value such as 123*10^4 how do i re-interpret that into matlab as 1230000 of course without typing it long ways, in other words i would like to accept integer and string/symbols/etc using inputdlg?
Thank you
Kind Regards
Scuba

采纳的回答

John D'Errico
John D'Errico 2023-3-13
编辑:John D'Errico 2023-3-13
You could just learn to use the standard scientific notation. So 123e4, which represents 123*10^4.
123e4
Or, you could see that is also 1.23e6, shifting the decimal yourself.
1.23e6
By the way, integer has only one r in it.
  2 个评论
Scuba
Scuba 2023-3-13
编辑:Scuba 2023-3-13
I would blame my OCD for always writing it in the style of x10^3 but i really should of tried using e notation first before posting that bible post... haha, thank you John much appreciated mate! Take care have a nice day/night.
and yeah thanks don't mind my spelling.
John D'Errico
John D'Errico 2023-3-13
The power form is common as people get started. It is the way we think about large numbers really. Something times 10 to some power. So that is what you write. A better habit is the scientific (e) form though. It is easier to write. For example, when you write 123*10^4, at least on my keyboard, I need to use two fingers for the * operator, since I need to type shift 8. The same thing applies for the ^ operator, which is a shift 6.
But now look at 123e4. Do you see you never needed to use two fingers even? So if for no other reason than it is simpler to type numbers, you should make that form a habit. Another way to look at it, is typing 123*10^4 requires 10 keystrokes since each operator is two keystrokes, whereas 123e4 requires only 5 keystrokes.
And, as much as I have been known to say that MATLAB does not charge extra for extra comments, if they want to give you an easy way to type faster, TAKE IT.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2023-3-13
The str2double function is a bit particular about the formats of text that it will convert to double. The format you've described doesn't satisfy it. On the other end of the spectrum is a plain call to str2num, but that can be subject to the "Bobby Tables" problem (aka arbitrary code execution.) Calling str2num with Evaluation="restricted" (assuming you're using release R2022a or later) may be sufficient for your needs.
s = '123*10^4'
s = '123*10^4'
n = str2num(s, Evaluation="restricted")
n = 1230000

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by