フィルターのクリア

How can i collect number without using eval function ?

2 ビュー (過去 30 日間)
ahmet ozdemir
ahmet ozdemir 2016 年 5 月 2 日
コメント済み: ahmet ozdemir 2016 年 5 月 2 日
I try to use int2str and num2str which didnot compute the problem. However, it computed ans = 51 43 50 If I give 3+2, it should compute 5 and show the screen which one can be defined ? What is something? Like example:
Number=input('Enter Number please:','s')
result=something(Number)
  4 件のコメント
CS Researcher
CS Researcher 2016 年 5 月 2 日
@ahmet ozdemir: Did you see my recent solution? It does what you are asking for.
ahmet ozdemir
ahmet ozdemir 2016 年 5 月 2 日
Thanks it works for plus but i want to work it for minus

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

採用された回答

CS Researcher
CS Researcher 2016 年 5 月 2 日
編集済み: CS Researcher 2016 年 5 月 2 日
You can try something like this:
in = input('Please enter a number:','s');
s = strsplit(in,'+');
result = sum(str2double(s));
  3 件のコメント
CS Researcher
CS Researcher 2016 年 5 月 2 日
編集済み: CS Researcher 2016 年 5 月 2 日
If it works you should accept my solution. You can update it to work for any operation. I will give you the solution to a basic 2 input problem:
in = input('Please enter a number:','s');
s = strsplit(in,'-');
result = -1*diff(str2double(s));
This will however only work for a basic subtraction. You should take it forward and modify it according to your need.
ahmet ozdemir
ahmet ozdemir 2016 年 5 月 2 日
Thanks for the answer. I fixed it for my needs and I accepted your solution.

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

その他の回答 (2 件)

Stephen23
Stephen23 2016 年 5 月 2 日
編集済み: Stephen23 2016 年 5 月 2 日
The safest and most versatile:
num = str2double(input('Please enter a number:','s'));

CS Researcher
CS Researcher 2016 年 5 月 2 日
You mean something like this:
Number=input('Enter Number please:','s');
eval(['result = ' Number])
  3 件のコメント
Stephen23
Stephen23 2016 年 5 月 2 日
1) the title states clearly "without using eval function"
2) eval should be avoided for many reasons. In this case it could be a security risk because it executes arbitrary code from the end user.
CS Researcher
CS Researcher 2016 年 5 月 2 日
Oh I am sorry. I misread the title.

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

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by