Problems accepting integer values with power and algebraic symbols using inputdlg

3 views (last 30 days)
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

Accepted Answer

John D'Errico
John D'Errico on 13 Mar 2023
Edited: John D'Errico on 13 Mar 2023
You could just learn to use the standard scientific notation. So 123e4, which represents 123*10^4.
Or, you could see that is also 1.23e6, shifting the decimal yourself.
By the way, integer has only one r in it.
John D'Errico
John D'Errico on 13 Mar 2023
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.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 13 Mar 2023
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
Scuba on 19 Mar 2023
I don't like errors poping up in my code so this eliminates that since the user can input e notation or other notation now.

Sign in to comment.


Find more on Licensing on Cloud Platforms in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!