How can i collect number without using eval function ?

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 comentarios

@ahmet ozdemir: can you please give exact input examples, and what you want the output to be. Do you want the user to be able to input any arbitrary equation and get the evaluated result, or do you want them to input numeric values?
Input should be only numberic value like 3+4+5 and output should show collection of these numbers like 12. I want only numberic values
@ahmet ozdemir: Did you see my recent solution? It does what you are asking for.
Thanks it works for plus but i want to work it for minus

Iniciar sesión para comentar.

 Respuesta aceptada

CS Researcher
CS Researcher el 2 de Mayo de 2016
Editada: CS Researcher el 2 de Mayo de 2016
You can try something like this:
in = input('Please enter a number:','s');
s = strsplit(in,'+');
result = sum(str2double(s));

3 comentarios

ahmet ozdemir
ahmet ozdemir el 2 de Mayo de 2016
Editada: ahmet ozdemir el 2 de Mayo de 2016
Thanks it works very-well if i put a minus from the equation like 3-5, what type of code i should add since code asks some expression and it includes some parenthesis like 3-(-9) i want result thanks a lot.
CS Researcher
CS Researcher el 2 de Mayo de 2016
Editada: CS Researcher el 2 de Mayo de 2016
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.
Thanks for the answer. I fixed it for my needs and I accepted your solution.

Iniciar sesión para comentar.

Más respuestas (2)

Stephen23
Stephen23 el 2 de Mayo de 2016
Editada: Stephen23 el 2 de Mayo de 2016
The safest and most versatile:
num = str2double(input('Please enter a number:','s'));
You mean something like this:
Number=input('Enter Number please:','s');
eval(['result = ' Number])

3 comentarios

Yes but I donot use eval function what is the another way?
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.
Oh I am sorry. I misread the title.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 2 de Mayo de 2016

Comentada:

el 2 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by