Scilab is quite powerful in handling string variables. Taking into account that sooner or later the Scilab user will need to define and format strings in Scilab, we should have a solid understanding on how strings can be defined and which are the main predefined function for handling.
Defining strings in Scilab
By definition, in the programming/coding world, a string is any finite sequence of characters. The characters can be either letters, numbers, symbols or punctuation marks. The complete list of characters is given by the ASCII (American Standard Code for Information Interchange) standard.
In Scilab, a string is delimited by either single quotes (e.g.
'blue') or double quotes (e.g.
"green") and it’s characterized by its length.
Let’s define the following strings in Scilab:
sVar1 = 'w'; sVar2 = 'vehicle'; sVar3 = 'Medium & heavy trucks and buses'; sVar4 = ['Cars, light trucks, motorcycles' '238656707';.. 'Medium & heavy trucks and buses' '11731405';.. 'Water' '12479438';.. 'Air' '230801';.. 'Rail' '30943'];
As you can see, a string can be defined as a single character (
sVar1) or a word (
sVar2). In the same variable we can also define a sentence (
sVar3) or even a matrix of strings (
Basic string operations
Three basing operations performed on a string are:
- string concatenation
- length measurement
- changing the case (upper, lower)
String concatenation means putting together (aside) two or more strings. It can be performed using the addition operator (
The length of the string can be measured using the predefined
length() function and it will return the number of ASCII characters in the string:
If applied to a vector or matrix of strings, the
length() function will return the length of each string in a numerical matrix format:
In order to check if a Scilab variable is a string, we can use the predefined function
typeof(). Calling it with a variable argument must return
Sometimes we might need to convert the character of a string to either upper case or lower case characters. For this we can use the predefined Scilab function
convstr(), with the
'u' flag for upper case or
'l' flag for lower case. Example:
MEDIUM & HEAVY TRUCKS AND BUSES
medium & heavy trucks and buses
Splitting and merging back strings
There are also predefined Scilab functions for splitting a phrase or sentence into individual words, or a word into characters.
A single word string can be split into individual characters by using the predefined Scilab function
--> tempVar = strsplit(sVar2)
The output will be a vector of strings, each element containing on character of the word. Obviously, the size of the vector will be equal with the length of the string.
To reconstruct the string back, starting from the vector of characters, we can use the function
Observation: If the string contains a blank space, it will also be stored in an element of the vector.
If we have a string which contains multiple words, like a sentence or phrase, we can split it in several words with the function
--> tempVar = tokens(sVar3)
tokens() function doesn’t store blank spaces, if we concatenate back the vector of words, we’ll get back a totally different string that the original one.
Find and Replace
Another very useful function is the Find (Search) and Replace function, which is common in all text editors. In Scilab we have a predefined function called
strsubst(), which care search within a string variable, a particular character or word, and replace it with another character or word (both strings).
Let’s suppose that we want to replace the blank spaces in
sVar3 with lines
'-'. To do this, we call the
strsubst() function with the following arguments: string variable name, string to be replaced, new string, as:
--> strsubst(sVar3,' ','-')
If we apply the same function to matrix of strings, every string element of the matrix will be processed and the replacement executed.
The string to be replaced and the new string, can be either characters, words or another string:
Medium and heavy trucks and buses
Conversion to ASCII code
Scilab provides built-in functions for conversion of characters in ASCII code and vice-versa. By calling the function
ascii(), having as argument a string, we get back the corresponding ASCII code.
If we provide a string variable which contains a word or several words, the
ascii() function will return the ASCII code for each character:
118. 101. 104. 105. 99. 108. 101.
The reverse function, conversion from ASCII code to string, is done using the Scilab function
char(). In the Scilab script below we are going to display all the ASCII characters. The script will display in the Scilab console 32 characters on each line, jump on the next line and keep displaying.
clc lineIdx = 0; for i=1:256 if (lineIdx < 33) printf("%s",char(i-1)); lineIdx = lineIdx + 1; else printf("\n%s",char(i-1)); lineIdx = 0; end end
Running the instructions, gives:
First 32 characters of the ASCII standard are not printable so that is why the first line is empty (it’s actually containing blank characters).
You can also display the ASCII characters in a table like format, using the script below. Adjust the characters you want to display by changing the
clc startLoop = 33; endLoop = 50; for i=startLoop:endLoop if i==startLoop printf("\nASCII code \t Character\n"); printf("%d \t\t %s\n",i,char(i)); else printf("%d \t\t %s\n",i,char(i)); end end
Running the instructions, gives:
ASCII code Character
Evaluating (interpreting) strings as Scilab instructions
Scilab strings can be interpreted (evaluated) as instructions with the help of
execstr() function. If we define a string as a simple mathematical addition of two real numbers, using
execstr() we can evaluate the string as an operation.
--> execstr('tempVar = 2.5 + 4')
For example, we can also use the
execstr() function to create (build) Scilab variables dynamically. Let’s suppose that we want to display in the Scilab console all our variables (
sVar4) automatically, with a script.
for i=1:4 execstr(strcat(['disp(sVar' string(i) ')'])) end
How does this work? With the
string() function we convert the integer variable
i into a string. With the
strcat() function we create our string to be evaluated. The
for loop will execute 4 strings, one after the other, the result being:
Medium & heavy trucks and buses
!Cars, light trucks, motorcycles 238656707 !
!Medium & heavy trucks and buses 11731405 !
!Water 12479438 !
!Air 230801 !
!Rail 30943 !
sVar4 contains the number of self-propelled vehicles, function of type, in the U.S. The source of the data is the U.S. Department of Transportation, Bureau of Transportation Statistics: National Transportation Statistics, Table 1-11: Number of U.S. Aircraft, Vehicles, Vessels, and Other Conveyances. Last update of the data was on 05.08.2013.
Notes on the data:
- Medium and heavy trucks and buses includes combination trucks, trucks with 2-axles and more than 6 tires, motor buses, bus, trolley bus, demand response vehicles, and other transit vehicles
- Rail includes, light rail locomotives, Amtrak locomotives, and Class I locomotives
- Water includes self-propelled vessels and recreational boats
We need to create a script that will output, as a string, the total number of non-earth moving vehicles. The script should interpret the strings automatically and return the result.
[rows columns] = size(sVar4); for i=1:rows if strindex(sVar4(i,1),'Air') idxA = i; end if strindex(sVar4(i,1),'Water') idxW = i; end end totalNonEarth = string(eval(sVar4(idxA,2)) + eval(sVar4(idxW,2)));
How does the script works ? First, we get the size of the matrix. Second, we go through each row and check if the string in column
1 is either
'Water'. If true, we save the index of the row. After we finish the
for loop, with the function
eval(), we convert the string containing the number of vehicles in an actual numerical value. We sum up the total number and convert it back to a string.
Running the script will calculate the total number of non-earth moving vehicle as:
Even if we change the position of the
'Water' strings in the matrix, the script will identify them and come up with the same total number.
There are a few more Scilab functions for string manipulation but mastering the above ones will give the user a solid understanding for further development.
For any questions, observations and queries regarding this article, use the comment form below.
Don’t forget to Like, Share and Subscribe!