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!