Problems accepting integer values with power and algebraic symbols using inputdlg

조회 수: 2(최근 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

채택된 답변

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.
Or, you could see that is also 1.23e6, shifting the decimal yourself.
By the way, integer has only one r in it.
  댓글 수: 2
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
  댓글 수: 2
Scuba 2023년 3월 19일
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.

댓글을 달려면 로그인하십시오.


Community Treasure Hunt

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

Start Hunting!

Translated by