You are on page 1of 18

www.openeering.

com
powered by
HOW TO DEVELOP A GRAPHICAL USER INTERFACE
(GUI) IN SCILAB.
In this tutorial we show how to create a GUI in Scilab for an ODE problem.
The adopted problem is the LHY model already used in other tutorials.
Level


This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.



LHY Tutorial Gui www.openeering.com page 2/18
Step 1: The purpose of this tutorial
In this tutorial we show, step by step, how to create a Graphical User
Interface (GUI)in Scilab.

Examples of Scilab GUIs are reported on the right. These images are
taken from the GUI menu of the Scilab Demonstrations.









Step 2: Model description
In this tutorial we refer to the LHY model. A description of the LHY model
and its solution using ode command can be found in the basic tutorial of
modeling differential equations in Scilab.






LHY Tutorial Gui www.openeering.com page 3/18
Step 3: Roadmap
In this tutorial we describe how to construct a GUI for the LHY. We
implement the system in the following way:
Provide a description of GUI programming in Scilab;
Implement the GUI for the LHY model;
Test the program and visualize the results.






Descriptions Steps
GUI programming in Scilab 4-5
GUI for the LHY model 6-17
Running and testing 18-19


Step 4: Scilab GUI programming
A GUI is a human-computer interface that uses graphical objects like
windows, menus and icons to interact with users through the use of
mouse and keyboard (often in a limited way).

The main advantage of using GUIs is the ability to make computer
operations more intuitive, and thus easier to learn and use especially for
new users. For example, it is much easier to move files by dragging icons
with the mouse than by having to remember the command that performs
the same task.

The function that creates a graphical user interface object in Scilab is
"uicontrol".


The syntax of the GUI function is

h = uicontrol(PropertyName,PropertyValue,...)
h = uicontrol(parent,PropertyName,PropertyValue,...)
h = uicontrol(uich)


For a detailed description see "hel p ui cont r ol ".


LHY Tutorial Gui www.openeering.com page 4/18
Step 5: Design of the GUI interface
The first step is to design the skeleton of the GUI interface for the LHY
model.

Designing a correct GUI is not a trivial task. Many rules exist and can be
found in textbooks and web sites dedicated to this subject.

On the right we report our scheme for the LHY graphical user interface
where:
"Model par amet er s" contains the list of all LHY model
parameters that the user can change;
"Ti me Set t i ng" contains the list of all time simulation variables;
"I ni t i al condi t i ons" contains the list of all initial conditions
for the ODE problem;
"gr aphi cal pl ot " contains the plot of the simulations results;
"Comput e but t on" updates the solution in the "gr aphi cal
pl ot ";
The menu "f i l e" contains the entry "cl ose" that is used to close
the program;
The menu "About " contains the entry "About " that is used to
display some info about program and author.








LHY Tutorial Gui www.openeering.com page 5/18
Step 6: Storing default parameters
The first step is to store all the default LHY model parameters into ml i st
Scilab objects. The parameters are subdivided into two groups:
the "par am" group for the LHY model parameters;
the "si m" group for the simulation time parameters and initial
conditions.

// Default simulation parameters
par am= [ ] ;
par am. t au = 5e4; // Number of innovators per year (initiation)
par am. s = 0. 61; // Annual rate at which light users attract non-
users (initiation)
par am. q = 3. 443; // Constant which measures the deterrent effect
of heavy users (initiation)
par am. smax = 0. 1; // Lower bound for s effective (initiation)
par am. a = 0. 163; // Annual rate at which light users quit
par am. b = 0. 024; // Annual rate at which light users escalate to
heavy use
par am. g = 0. 062; // Annual rate at which heavy users quit
par am. del t a = 0. 291; // Forgetting rate

// Simulation data
// Time variables
si m= [ ] ;
si m. Tbegi n = 1970; // Initial time
si m. Tend = 2020; // Final time
si m. Tst ep = 1/ 12; // Time step
// Initial conditions
si m. L0 = 1. 4e6; // Light users at the initial time
si m. H0 = 0. 13e6; // Heavy users at the initial time
si m. Y0 = 0. 11e6; // Decaying heavy user at the initial time

Thenameofthedevelopedfunctionis"LHY_Gui ".







LHY Tutorial Gui www.openeering.com page 6/18
Step 7: Create an initial window
In the next step we create, with the "scf " Scilab function, an initial window
where graphical object can be added.
The function "get t ext " is used for internationalization (a.k.a. i18n, i.e. it
translates the text into the current local language if available).

// Global window parameters
gl obal mar gi n_x mar gi n_y;
gl obal f r ame_w f r ame_h pl ot _w pl ot _h;

// Window Parameters initialization
f r ame_w = 300; f r ame_h = 550; // Frame width and height
pl ot _w = 600; pl ot _h = f r ame_h; // Plot width and heigh
mar gi n_x = 15; mar gi n_y = 15; // Horizontal and vertical margin
for elements
def aul t f ont = " ar i al " ; // Default Font
axes_w = 3*mar gi n_x + f r ame_w + pl ot _w; // axes width
axes_h = 2*mar gi n_y + f r ame_h; // axes height (100 =>
toolbar height)

demo_l hy = scf ( 100001) ; // Create window with id=100001 and make
it the current one


// Background and text
demo_l hy. backgr ound = - 2;
demo_l hy. f i gur e_posi t i on = [ 100 100] ;
demo_l hy. f i gur e_name = get t ext ( " LHY sol ut i on" ) ;

// Change dimensions of the figure
demo_l hy. axes_si ze = [ axes_w axes_h] ;

Type help scf for more details on the scf command.






















LHY Tutorial Gui www.openeering.com page 7/18
Step 8: Set menu and toolbar
After creating the window, we create the menu. The used function are:
"addmenu"/"del menu": for adding or removing a menu from the
menu bar;
"ui menu": for creating a menu or a submenu in a figure.
Associate to each menu entry there is a callback, a reference to a
executable function that is activated when the user select the
entry.
// Remove Scilab graphics menus & toolbar
del menu( demo_l hy. f i gur e_i d, get t ext ( " &Fi l e" ) ) ;
del menu( demo_l hy. f i gur e_i d, get t ext ( " &Tool s" ) ) ;
del menu( demo_l hy. f i gur e_i d, get t ext ( " &Edi t " ) ) ;
del menu( demo_l hy. f i gur e_i d, get t ext ( " &?" ) ) ;
t ool bar ( demo_l hy. f i gur e_i d, " of f " ) ;

// New menu
h1 = ui menu( " par ent " , demo_l hy, " l abel " , get t ext ( " Fi l e" ) ) ;
h2 = ui menu( " par ent " , demo_l hy, " l abel " , get t ext ( " About " ) ) ;

// Populate menu: file
ui menu( " par ent " , h1, " l abel " , get t ext ( " Cl ose" ) , " cal l back" ,
" demo_l hy=get _f i gur e_handl e( 100001) ; del et e( demo_l hy) ; " ,
" t ag" , " cl ose_menu" ) ;

// Populate menu: about
ui menu( " par ent " , h2, " l abel " , get t ext ( " About " ) ,
" cal l back" , " LHY_About ( ) ; " ) ;
// Sleep to guarantee a better display (avoiding to see a
sequential display)
sl eep( 500) ;
An import field for the graphical function is the parent field i.e. the
handle where the graphical object should be attached, in our case
demo_lhy. Changing this value allows to move a control from a figure to
another.
See uimenu help file for more details.












LHY Tutorial Gui www.openeering.com page 8/18
Step 9: About dialog
The About dialog is a modal dialog created using the "messagebox"
function. Two types of windows are available:
"modal dialog": in a modal window users are required to interact
with it before it returns to the parent application.
"modeless dialog": in modeless or non-modal window the
request information is not essential to continue with the program.
A non-modal window can be left open while work continues
elsewhere.
f unct i on LHY_About ( )
msg = mspr i nt f ( get t ext ( " LHY i s devel oped by t he Openeer i ng
Team. \ nAut hor : M. Vent ur i n" ) ) ;
messagebox( msg, get t ext ( " About " ) , " i nf o" , " modal " ) ;
endf unct i on
















Step 10: Creating a frame
A frame is graphical object used to group other objects.
// Frames creation [LHY parameters]
my_f r ame = ui cont r ol ( " par ent " , demo_l hy, " r el i ef " , " gr oove" , . . .
" st yl e" , " f r ame" , " uni t s" , " pi xel s" , . . .
" posi t i on" , [ mar gi n_x mar gi n_y f r ame_w f r ame_h] , . . .
" hor i zont al al i gnment " , " cent er " , " backgr ound" , [ 1 1 1] , . . .
" t ag" , " f r ame_cont r ol " ) ;

// Frame title
my_f r ame_t i t l e = ui cont r ol ( " par ent " , demo_l hy, " st yl e" , " t ext " , . . .
" st r i ng" , " LHY par amet er s" , " uni t s" , " pi xel s" , . . .
" posi t i on" , [ 30+mar gi n_x mar gi n_y+f r ame_h- 10 f r ame_w- 60 20] ,
. . .
" f ont name" , def aul t f ont , " f ont uni t s" , " poi nt s" , . . .
" f ont si ze" , 16, " hor i zont al al i gnment " , " cent er " , . . .
" backgr ound" , [ 1 1 1] , " t ag" , " t i t l e_f r ame_cont r ol " ) ;





LHY Tutorial Gui www.openeering.com page 9/18
Step 11: Populate the frame
Each parameters is composed of the following elements:
a string that denote the parameter (style text);
an edit box (style edit) that is used as an input/output mask (the
input is recovering by searching all graphical objects having that
tag, and hence the tag must be unique among the elements)

// Adding model parameters
gui h1 = f r ame_w;
gui h1o = 240;

// ordered list of labels
l abel s1 = [ " Tau" , " s" , " q" , " smax" , " a" , " b" , " g" , " del t a" ] ;
// ordered list of default values
val ues1 = [ 5e4, 0. 61, 3. 443, 0. 1, 0. 163, 0. 024, 0. 062, 0. 291] ;
// positioning
l 1 = 40; l 2 = 100; l 3 = 110;
f or k=1: si ze( l abel s1, 2)
ui cont r ol ( " par ent " , demo_l hy, " st yl e" , " t ext " , . . .
" st r i ng" , l abel s1( k) , " posi t i on" , [ l 1, gui h1-
k*20+gui h1o, l 2, 20] , . . .
" hor i zont al al i gnment " , " l ef t " , " f ont si ze" , 14, . . .
" backgr ound" , [ 1 1 1] ) ;

gui ent r y1( k) = ui cont r ol ( " par ent " , demo_l hy, " st yl e" , " edi t " ,
. . .
" st r i ng" , st r i ng( val ues1( k) ) , " posi t i on" , [ l 3, gui h1-
k*20+gui h1o, 180, 20] , . . .
" hor i zont al al i gnment " , " l ef t " , " f ont si ze" , 14, . . .
" backgr ound" , [ . 9 . 9 . 9] , " t ag" , l abel s1( k) ) ;
end













LHY Tutorial Gui www.openeering.com page 10/18
Step 12: Continue with the frame
As previously done, we continue to add graphical components for
simulations parameters and initial conditions.
// Adding simulation paramters
gui h2 = 240;
gui h2o = 80;
l abel s2 = [ " Tbegi n" , " Tend" , " Tst ep" ] ;
val ues2 = [ " 1970" , " 2020" , " 1/ 12" ] ;
l 1 = 40; l 2 = 100; l 3 = 110;
f or k=1: si ze( l abel s2, 2)
ui cont r ol ( " par ent " , demo_l hy, " st yl e" , " t ext " , . . .
" st r i ng" , l abel s2( k) , " posi t i on" , [ l 1, gui h2-
k*20+gui h2o, l 2, 20] , . . .
" hor i zont al al i gnment " , " l ef t " , " f ont si ze" , 14, . . .
" backgr ound" , [ 1 1 1] ) ;

gui ent r y2( k) = ui cont r ol ( " par ent " , demo_l hy, " st yl e" , " edi t " ,
. . .
" st r i ng" , val ues2( k) , " posi t i on" , [ l 3, gui h2-
k*20+gui h2o, 180, 20] , . . .
" hor i zont al al i gnment " , " l ef t " , " f ont si ze" , 14, . . .
" backgr ound" , [ . 9 . 9 . 9] , " t ag" , l abel s2( k) ) ;
end
// Adding initial conditions
gui h3 = 150;
gui h3o = 80;
l abel s3 = [ " L0" , " H0" , " Y0" ] ;
val ues3 = [ 1. 4e6, 0. 13e6, 0. 11e6] ;
l 1 = 40; l 2 = 100; l 3 = 110;
f or k=1: si ze( l abel s3, 2)
ui cont r ol ( " par ent " , demo_l hy, " st yl e" , " t ext " , . . .
" st r i ng" , l abel s3( k) , " posi t i on" , [ l 1, gui h3-
k*20+gui h3o, l 2, 20] , . . .
" hor i zont al al i gnment " , " l ef t " , " f ont si ze" , 14, . . .
" backgr ound" , [ 1 1 1] ) ;

gui ent r y3( k) = ui cont r ol ( " par ent " , demo_l hy, " st yl e" , " edi t " ,
. . .
" st r i ng" , st r i ng( val ues3( k) ) , " posi t i on" , [ l 3, gui h3-
k*20+gui h3o, 180, 20] , . . .
" hor i zont al al i gnment " , " l ef t " , " f ont si ze" , 14, . . .
" backgr ound" , [ . 9 . 9 . 9] , " t ag" , l abel s3( k) ) ;
end







LHY Tutorial Gui www.openeering.com page 11/18
Step 13: Add a button
Next, using the same command uicontrol, we add the compute button
(style pushbutton):

// Adding button
hui but t on = ui cont r ol ( demo_l hy, " st yl e" , " pushbut t on" , . . .
" Posi t i on" , [ 110 100 100 20] , " St r i ng" , " Comput e" , . . .
" Backgr oundCol or " , [ . 9 . 9 . 9] , " f ont si ze" , 14, . . .
" Cal l back" , " syscomput e" ) ;

The button is associated to the callback "syscomput e". This callback will
be explained and created at step 17.




Step 14: Solving function
The solving function is based on the code already developed in the tutorial
of the LHY model solved using "ode" function. See this tutorial for more
details.


f unct i on sol=sol vi ngPr obl em( param, sim)
// Compute the solution
sol = st r uct ( ) ;

// Assign ODE solver data
y0 = [ sim. L0; sim. H0; sim. Y0] ;
t 0 = sim. Tbegi n;
t = sim. Tbegi n: sim. Tst ep: ( sim. Tend+100*%eps) ;
f = LHY_Syst em;

// Solving the system
sol. LHY = ode( y0, t 0, t , f ) ;
sol. t = t ;
endf unct i on



LHY Tutorial Gui www.openeering.com page 12/18
Step 15: Display function
The display functions is derived from the "LHY_Pl ot " function of the
tutorial on LHY solved using "ode" function. See this tutorial for more
details.




















f unct i on di spl ayPr obl em( solh)
// Fetching solution
LHY = sol . LHY;
t = sol . t ;
L = LHY( 1, : ) ; H = LHY( 2, : ) ; Y = LHY( 3, : ) ;

// Evaluate initiation
I = LHY_I ni t i at i on( L, H, Y, par am) ;

// maximum values for nice plot
[ Lmax, Li ndmax] = max( L) ; t L = t ( Li ndmax) ;
[ Hmax, Hi ndmax] = max( H) ; t H = t ( Hi ndmax) ;
[ Ymax, Yi ndmax] = max( Y) ; t Y = t ( Yi ndmax) ;
[ I max, I i ndmax] = max( I ) ; t I = t ( I i ndmax) ;

// Text of the maximum point
Lt ext = mspr i nt f ( ' ( %4. 1f , %7. 0f ) ' , t L, Lmax) ;
Ht ext = mspr i nt f ( ' ( %4. 1f , %7. 0f ) ' , t H, Hmax) ;
Yt ext = mspr i nt f ( ' ( %4. 1f , %7. 0f ) ' , t Y, Ymax) ;
I t ext = mspr i nt f ( ' ( %4. 1f , %7. 0f ) ' , t I , I max) ;

// Plotting of model data
del et e( gca( ) ) ;
pl ot ( t , [ LHY; I ] ) ;
l egend( [ ' Li ght User s' ; ' Heavy user s' ; ' Memor y' ; ' I ni t i at i on' ] ) ;

// Vertical line
set ( gca( ) , " aut o_cl ear " , " of f " ) ;

xpol ys( [ t L, t H, t Y, t I ; t L, t H, t Y, t I ] , [ 0, 0, 0, 0; Lmax, Hmax, Ymax, I max] ) ;

// Text of maximum point
xst r i ng( t L, Lmax, Lt ext ) ; xst r i ng( t H, Hmax, Ht ext ) ;
xst r i ng( t Y, Ymax, Yt ext ) ; xst r i ng( t I , I max, I t ext ) ;
xl abel ( ' Year ' ) ;
set ( gca( ) , " aut o_cl ear " , " on" ) ;

my_pl ot _axes = gca( ) ;
my_pl ot _axes. t i t l e. f ont _si ze = 5;
my_pl ot _axes. axes_bounds = [ 1/ 3, 0, 2/ 3, 1] ;
my_pl ot _axes. t i t l e. t ext = " LHY sol ut i on" ;
endf unct i on


LHY Tutorial Gui www.openeering.com page 13/18
Step 16: Default initial plot
The last step for "LHY_Gui " is to compute the solution with the default
data and to re-draw the window.




// Compute solution
sol = sol vi ngPr obl em( par am, si m) ;

// Redraw window
dr awl at er ( ) ;
newaxes( ) ;
di spl ayPr obl em( sol ) ;
dr awnow( ) ;





















LHY Tutorial Gui www.openeering.com page 14/18
Step 17: Linking the computational function to the button
The "syscomput e" function is associated to the Compute button (see
step 13) and performs the following steps:
it retrieves data from the object framework: this process is done
by the function "f i ndobj " that searches from all graphical objects
those having the prefixed target tag;
it evaluates the available data in the field converting these data
from string into real using the function "evst r ";
it performs the simulation with the "sol vi ngPr obl em" function;
it redraws the window.



f unct i on syscomput e
// retrive data
par am= [ ] ;
t au = f i ndobj ( " t ag" , " Tau" ) ; par am. t au = evst r ( t au. st r i ng) ;
s = f i ndobj ( " t ag" , " s" ) ; par am. s = evst r ( s. st r i ng) ;
q = f i ndobj ( " t ag" , " q" ) ; par am. q = evst r ( q. st r i ng) ;
smax = f i ndobj ( " t ag" , " smax" ) ; par am. smax =
evst r ( smax. st r i ng) ;
a = f i ndobj ( " t ag" , " a" ) ; par am. a = evst r ( a. st r i ng) ;
b = f i ndobj ( " t ag" , " b" ) ; par am. b = evst r ( b. st r i ng) ;
g = f i ndobj ( " t ag" , " g" ) ; par am. g = evst r ( g. st r i ng) ;
del t a = f i ndobj ( " t ag" , " del t a" ) ; par am. del t a =
evst r ( del t a. st r i ng) ;

si m= [ ] ;
Tbegi n = f i ndobj ( " t ag" , " Tbegi n" ) ; si m. Tbegi n =
evst r ( Tbegi n. st r i ng) ;
Tend = f i ndobj ( " t ag" , " Tend" ) ; si m. Tend = evst r ( Tend. st r i ng) ;
Tst ep = f i ndobj ( " t ag" , " Tst ep" ) ; si m. Tst ep =
evst r ( Tst ep. st r i ng) ;
L0 = f i ndobj ( " t ag" , " L0" ) ; si m. L0 = evst r ( L0. st r i ng) ;
H0 = f i ndobj ( " t ag" , " H0" ) ; si m. H0 = evst r ( H0. st r i ng) ;
Y0 = f i ndobj ( " t ag" , " Y0" ) ; si m. Y0 = evst r ( Y0. st r i ng) ;

// Compute solution
sol = sol vi ngPr obl em( par am, si m) ;

// Redraw window
dr awl at er ( ) ;
my_pl ot _axes = gca( ) ;
my_pl ot _axes. t i t l e. t ext = " LHY sol ut i on" ;
newaxes( ) ;
di spl ayPr obl em( sol ) ;
dr awnow( ) ;
endf unct i on





LHY Tutorial Gui www.openeering.com page 15/18
Step 18: Running and testing #1
Change the Scilab working directory to the current project working
directory and run the command exec( ' LHY_Mai nGui . sce' , - 1) .
This command runs the main program and plots a chart similar to the one
shown on the right.






Step 19: Running and testing #2
Since we have used the function "evst r " it is possible to insert
expression in the input masks and see, in an easy way, the effects of
parameters changing in the solution.
For example, on the right we have reported the simulation where the
parameters b = u.u24 - S.2 and = u.291 +exp (-S.2).





LHY Tutorial Gui www.openeering.com page 16/18
Step 20: Exercise #1
Try to modify the program adding the following feature: Every time the
compute button is pressed the program check if the LHY parameters are
changed with respect to the default values and if a parameter is changed
then the associated input mask is colored using the orange color as
depicted in the figure.





Step 20: Exercise #1 (Hints)
Some hints:
copy the mlist data "par am" and "si m" into a global data structure
and save the name of their fields;
add a "updat edFi el ds" function into the "syscomput e"
function;
write the "updat edFi el ds" function.



// global default data
gl obal gpar amgsi mpf sf ;
gpar am= par am;
gsi m= si m;
// param and sim fields
pf = f i el dnames( gpar am) ; // param fields
sf = f i el dnames( gsi m) ; // sim fields


// Update fields
updat eFi el ds( par am, si m) ;



LHY Tutorial Gui www.openeering.com page 17/18

function updateFields(param, sim)
global gparam gsim pf sf

// Loop over gparam/param elements
for i=1:size(pf,1)
// Get current field
cf = pf(i);
// check if they are different
checkequal = 0;
if(abs(gparam(cf)-param(cf))<= 1e-12) then
checkequal = 1;
end
// Find object
obj = findobj("tag", cf);
if(isempty(obj)) then
// object not found, skip, some problems?
continue;
end
// Colorize
if(~checkequal) then
// Object is modified: use new color
obj.BackgroundColor = [0.98 0.83 0.71];
else
// Object is not modified: use default color
obj.BackgroundColor = [.9 .9 .9];
end
end

// The same code for the field gsim/sim
endfunction


Step 21: Concluding remarks and References
In this tutorial we have shown how a GUI can be developed for the LHY
model.
On the right-hand column you may find a list of references for further
studies.

1. Scilab Web Page: Available: www.scilab.org.
2. Openeering: www.openeering.com.

LHY Tutorial Gui www.openeering.com page 18/18
Step 22: Software content
To report a bug or suggest some improvement please contact Openeering
team at the web site www.openeering.com.





Thank you for your attention,
Manolo Venturin


- - - - - - - - - - - - - - - - - - -
GUI MODEL I N SCI LAB
- - - - - - - - - - - - - - - - - - -


- - - - - - - - - - - - - - - -
Di r ect or y: model
- - - - - - - - - - - - - - - -
LHY_Col or _Gui . sci : GUI f or t he LHY model ( ex. 1)
LHY_Gui . sci : GUI f or t he LHY model
LHY_I ni t i at i on. sci : I ni t i at i on f unct i on
LHY_Syst em. sci : LHY syst em

- - - - - - - - - - - - - -
Mai n di r ect or y
- - - - - - - - - - - - - -
LHY_Mai nGui . sce : Mai n consol e GUI pr ogr am
LHY_Mai nGui Col or . sce : Sol ut i on of t he exer ci se
l i cense. t xt : The l i cense f i l e

You might also like