如果我得到你的意圖吧,你想創建一個自定義菜單。這裏是一個小的演示,如何建立自定義菜單元素,以及如何使用locate
獲取座標(但不需要xclick
來感應菜單點擊)。這段代碼並沒有明確地使用cbmenu,但沒有必要讓它工作。如果您運行代碼,首先單擊「選擇範圍」菜單,然後在圖上單擊兩次。然後「迴歸」菜單被激活,您可以選擇「線性迴歸」。通過點擊「退出程序」,執行中止。並非所有nenu功能都已實施,它們僅用於演示多層結構。我希望它有幫助!
//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
我很熟悉的菜單和用他們幾次。我只關注'cbmenu'的功能。我能用它做什麼?唯一的信息是 - 正如我所引用的:cbmenu:String:如果xclick由於點擊菜單而返回,則與菜單關聯的回調。 – Rosestock
我對菜單非常熟悉,並多次使用它們。我只關注'cbmenu'的功能。我能用它做什麼? ***唯一的信息是 - 正如我所引用的:'cbmenu:String:如果xclick由於點擊菜單返回而與菜單關聯的回調。' *** Sclab文檔經常會產生糟糕的英語。如果你理解這個短語,請通過重新編輯或編輯我的代碼片段來改進它,以使它具有某種功能***我的三顆星指示新的段落。返回鍵似乎做了別的。 – Rosestock