Scilab: How to use cbmenu in xclick()

370 views Asked by At

I do not understand the explanation of the output argument cbmenu in the Scilab documentation of xclick.

It says:

cbmenu: String: callback associated to a menu if xclick returns due to a click on a menu.

I did not find any example in the web so I ask here. I have coded a snippet which lumps together the elements which may be relevant for cbmenu. The snippet does nothing, when I click on CLICK. Could anyone alter/complement/revise the code so that it does something - whatever it is to give me a clue what could be done with cbmenu?

xdel()
x=[-1 +1];
cf=figure(0);
plot(x,x)
m=uimenu(cf,'label','CLICK','callback','two=1+1');
[ibutton,xcoord,yxcoord,iwin,cbmenu]=xclick();

Kind regards

Rosestock

2

There are 2 answers

2
Attila On

If I get your intentions right, you want to create a custom menu. Here is a small demo, how to bulid custom menu elements, and also how to use locate to get back coordinates (but you don't need xclick to sense menu clicking). This code does not use explicitly cbmenu, but it is not necessary to get it work. If you run the code, first click the "Select range" menu, then click two times on the graph. Then the "Regression" menu is activated, and you can choose "Linear regession". By clicking "Exit program", the execution aborts. Not all nenu functions are implemented, they are there only to demonstrate a multilayer structure. I hope it helps!

//Interactive menu demo
//  Scilab 5.5.2
//  2016.12.20. Attila Eredics
//
//Attention!
//Do not close the graphic window with the original x symbol 
//  while the program is running,
//  because the program will run infinitely!
//  But you can stop it in the Consol with: Ctrl+C and then typing: abort
clc;
clear;
lines(0);

//artificial data vectors
row = 100;
t=gsort(rand(row,1),'r','i');    //time
c=10*t+8;       //concentration
c=c+rand(c);    //add some noise

//graphic window
scf(0); clf(0);
w=gcf();    //get current figure

//new menu elements
m3=uimenu(w,"Label","Select range","Callback","m=3");
m4=uimenu(w,"Label","Regression","Enable","off","ForegroundColor","0.5|0.5|0.5");
m41=uimenu(m4,"Label","Linear","Callback","m=41","Enable","on");
m42=uimenu(m4,"Label","Nonlinear","Callback","m=42","Enable","on");
m5=uimenu(w,"Label","Save results","Callback","m=5");
m6=uimenu(w,"Label","Exit program","Callback","abort","ForegroundColor","1|0|0");

plot2d(t,c,style=-5,frameflag=6);    //plot the data
xtitle("","time","concentration");  //axis labels

while %T   //infinite loop
    //-------------wait for user action (click on a menu)-------
    m=0;
    while m<1
        xpause(1e4);    //pause 10ms
    end

    //------------------the menu actions-------------------------
    if m==3 then    //Select range
        mprintf("\nChoose 2 points (click on the graph)!");
        coord=locate(2);    //locate two clicks
        disp(coord);        //coord: according to the axes

        //Select range in t vector
        for i=1:row-1
            if coord(1,1)>=t(i) & coord(1,1)<t(i+1) then    //t(i) <= 1st click < t(i+1)
                i1=i;
            end
            if coord(1,2)>=t(i) & coord(1,2)<t(i+1) then    //t(i) <= 2nd click < t(i+1)
                i2=i;
            end
        end

        //selected range
        clear tt;
        clear cc;
        tt=t(i1:i2);
        cc=c(i1:i2);

        plot2d(tt,cc,style=-5);    //plot selected points
        h2=gce();
        h2.children.mark_foreground=3;    //green

        m4.Enable="on";    //enable Regression menu
        m4.ForegroundColor="0|0|0"
        m3.Enable="off";    //Disable selection menu
        m3.ForegroundColor="0.5|0.5|0.5"
    elseif m==41 then    //linear regression of the selected points
        mprintf("\nLinear regression");
        [a,b,sig]=reglin(tt',cc');

        plot2d([tt(1),tt($)],a*[tt(1),tt($)]+b,style=2);    //blue line

        m41.Enable="off";    //disable Linear regression menu
        m41.ForegroundColor="0.5|0.5|0.5"
    elseif m==42 then    //Nonlinear regression
        mprintf("\nNot implemented yet!");
    elseif m==5 then    //Save
        mprintf("\nNot implemented yet!");
    end

end
1
Rosestock On

This is a minimal script which explains what the 5th output argument of xclick() can do:

xdel()
x=[-1 +1];
winnum=1;  win=string(winnum);
cf=figure(winnum);
plot(x,x)
C=["Green" "Red" "Abort"];//case name strings
addmenu(winnum, C(1)); C1="execstr("+C(1)+"_"+win+"(1))";   
addmenu(winnum, C(2)); C2="execstr("+C(2)+"_"+win+"(1))";
addmenu(winnum, C(3)); C3="execstr("+C(3)+"_"+win+"(1))";
while %t
  [!,!,!,!,cbmenu]=xclick();
  //Get the clicked option by cbmenu
  if cbmenu==C1, cf.background=3; end
  if cbmenu==C2, cf.background=5; end
  if cbmenu==C3, break, end
end