Wednesday, December 24, 2014

Keyboard input in running time

MERRY CHRISTMAS!

Dear readers, I hope you have a merry Christmas and a great new year!

This is my last post in 2014, thus I'd like to say THANK YOU, for all my readers, this blog has been much visited and became a relevant reference for Scilab.

This blog is the concretization of my will to share some knowledge about Scilab, and it's now a reference for people around the World.

But, coming to the technical content, let's see something about using keyboard for input data in running time.

Scilab has the function input(), that holds the script execution and returns anything is typed in the keyboard.

Try this line in the Scilab console:

-->x = input("x will be: ");
x will be: 2

-->x
 x  =

    2.

The value 2 was inserted from the keyboard, you can try any value and after use the variable x in following commands.

If you want to input a string, there are two ways:

single or double quotes when typing:

-->x = input("x will be: ");
x will be: 'hi'

-->x
 x  =

 hi  

-->x = input("x will be: ");
x will be: "hello"

-->x
 x  =

 hello  


Or, you can use a second argument "string":

-->x = input("x will be: ", "string");
x will be: yes!

-->x
 x  =

 yes!


If you try to type text without single or double quotes, there will happen an error:

-->x = input("x will be: ");
x will be: test
Undefined variable: test
x will be: 0


So, it's all!

Let's stay using, studying and improving Scilab in 2015!

Wednesday, October 8, 2014

Native Function for Parsing String to Math Expression

My last post was about parsing strings to math functions, like:

parsingStr2Arithmetics("2+2")
 ans  =

    4.  

But, Scilab has a native function that evaluates strings:

evstr()

There are some examples using this function:

-->evstr("1")
 ans  =

    1.

-->evstr("1+1")
 ans  =

    2.

-->evstr("2*(1+8)/3")
 ans  =

    6.

-->evstr("2*(1+1)")
 ans  =

    4.

-->evstr("2*(1+1) a")
%val=[2*(1+1) a;
                !--error 4
Variável indefinida: a
in  execstr instruction    called by :
at line      35 of function evstr called by :
evstr("2*(1+1) a")


Take a look that last example ahs failed, it happened because the argument "2*(1+1) a" does not mean a valid expression.

Thus, there is a optimized way to do this task and work with strings which mean math expressions.

Another useful use for this function is to use it for running another functions, for example:

-->evstr("exp(1)")
 ans  =

    2.7182818

-->evstr("sin(%pi/2)")
 ans  =

    1.

-->evstr("zeros(5, 5)")
 ans  =

    0.    0.    0.    0.    0.
    0.    0.    0.    0.    0.
    0.    0.    0.    0.    0.
    0.    0.    0.    0.    0.
    0.    0.    0.    0.    0.

To put a string in the argument, there is needed to use an inside single quote ', like this:

-->"'"my string'""
 ans  =

 "my string"  


So, the function evstr() may evaluate strings with other strings inside, like this:

-->evstr("'"my'"+ '" string'"")
 ans  =

 my string  

-->evstr("length('"test'")")
 ans  =

    4.

-->evstr("strsplit('"this is a string inside'", '" '")")
 ans  =

!this    !
!        !
!is      !
!        !
!a       !
!        !
!string  !
!        !
!inside  !

Think out of the box, strings may mean math and any other functions or sentence!

Wednesday, November 6, 2013

Parsing string to math expression with inline function

This week, I discovered a new math game for Android: Match The Math (https://play.google.com/store/apps/details?id=com.engcross.matchthemath).

I have seen this app converts a string to a math expression and solves it in real-time. So I though about how doing it in Scilab, to get a string like "2*5 + 2" and return the number that corresponds to the result of the expression, in the example 12 (= 2*5 + 2).

Scilab has an interesting way to create "one line" functions, e. g. sum2numbers(a,b) where this function should return just a+b. This way is called inline functions and is defined like following:

deff('[x]=func(a,b)', 'x=a+b');

Where '[x]=func(a,b)' defines the structure of the function with x being the return, func is the name of the function and a and b are the arguments; 'x=a+b' is what the function executes when is called: x=a+b.

With this concept in mind, we can use a inline function to parse a string to a arithmetic expression because deff() uses only strings as arguments.

Let's use a common function definition, with a string to be parsed as argument, and one inline function inside:

function [x]=parsingStr2Arithmetics(s) // definition of the function
deff('[x]=f()', 'x='+s); // creates inline function that parses the string
x = f(); // calls the function to return it result
endfunction; //end of the function



Now our function is ready, we can test it using different expressions as arguments.

-->parsingStr2Arithmetics("2")
 ans  =

    2. 

-->parsingStr2Arithmetics("2+2")
 ans  =

    4. 

-->parsingStr2Arithmetics("2+2*5")
 ans  =

    12. 
-->parsingStr2Arithmetics("(2+2)*5")
 ans  =

    20. 

-->parsingStr2Arithmetics("(2+2)*5 - 3")
 ans  =

    17. 


For finishing this post, I recommend you to try Match The Math game, it's really cool and improves our math and problems solving skills.