Scilab - gui - 在调整滑块期间自动调整图形...?

时间:2017-12-08 13:56:22

标签: scilab

我试图在scilab中创建一个简单的GUI,只是为了显示函数a' sin(x)的图形,其中滑块正在驱动" a"的值。我似乎找不到一种方法来只显示对应于" a" ...的当前值的曲线...它只是将新曲线添加到现有曲线。我希望有一个简单的解决方案,因为我现在无法进一步(我只有Mathematica的经验)。我使用的非常简单的代码是:

// This GUI file is generated by guibuilder version 3.0
//////////
f=figure('figure_position',[400,50],'figure_size',
[656,581],'auto_resize','on','background',[33],'figure_name','Graphic 
window number %d');
//////////
delmenu(f.figure_id,gettext('File'))
delmenu(f.figure_id,gettext('?'))
delmenu(f.figure_id,gettext('Tools'))
toolbar(f.figure_id,'off')
handles.dummy = 0;
handles.slider=uicontrol(f,'unit','normalized','BackgroundColor',
[-1,-1,-1],'Enable','on','FontAngle','normal','FontName','Tahoma',
'FontSize',[12],'FontUnits','points','FontWeight','normal',
'ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left','ListboxTop',
[],'Max',[1],'Min',[0],'Position',[0.0359375,0.78125,0.2625,0.09375],
'Relief','default','SliderStep',
[0.01,0.1],'String','slider1','Style','slider','Value',
[0],'VerticalAlignment','middle','Visible','on','Tag','slider',
'Callback','slider_callback(handles)')
handles.graph= newaxes();handles.graph.margins = [ 0 0 0 
0];handles.graph.axes_bounds = [0.31875,0.01875,0.6640625,0.925];

//////////
// Callbacks are defined as below. Please do not delete the comments as 
it will be used in coming version
//////////

function slider_callback(handles)
//Write your callback for  slider  here
x=0:0.1:10;
a=handles.slider.Value;
plot(x,a*sin(x));
endfunction

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

默认情况下,每个图形指令都会将其结果添加到当前显示中 如果希望在显示新图形指令之前自动擦除图形,可以将轴的自动清除属性设置为“打开”。另一种解决方案是使用删除功能擦除上一个绘图。

如果您不想显示刻度,可以使用xpoly函数代替绘图函数

// This GUI file is generated by guibuilder version 3.0
//////////
f=figure('figure_position',[400,50],'figure_size',...
[656,581],'auto_resize','on','background',[33],'figure_name','Graphic window number %d');
//////////
delmenu(f.figure_id,gettext('File'))
delmenu(f.figure_id,gettext('?'))
delmenu(f.figure_id,gettext('Tools'))
toolbar(f.figure_id,'off')
handles.dummy = 0;
handles.slider=uicontrol(f,'unit','normalized','BackgroundColor',...
[-1,-1,-1],'Enable','on','FontAngle','normal','FontName','Tahoma',...
'FontSize',[12],'FontUnits','points','FontWeight','normal',...
'ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left','ListboxTop',...
[],'Max',[1],'Min',[0],'Position',[0.0359375,0.78125,0.2625,0.09375],...
'Relief','default','SliderStep',...
[0.01,0.1],'String','slider1','Style','slider','Value',...
[0],'VerticalAlignment','middle','Visible','on','Tag','slider',...
'Callback','slider_callback(handles)')
handles.graph= newaxes();
handles.graph.margins = [ 0 0 0 0];
handles.graph.axes_bounds = [0.31875,0.01875,0.6640625,0.925];
handles.graph.data_bounds=[0 -1.2;10 1.2];
handles.graph.auto_clear="on";
//////////
// Callbacks are defined as below. Please do not delete the comments as 
//it will be used in coming version
//////////

function slider_callback(handles)
//Write your callback for  slider  here
x=0:0.1:10;
a=handles.slider.Value;
drawlater()
//delete(handles.graph.children)
xpoly(x,a*sin(x))
handles.graph.data_bounds=[0 -1.2;10 1.2];
drawnow
//plot(x,a*sin(x));
endfunction

答案 1 :(得分:0)

您可以访问图形的折线并直接更改数据(x,y)。 将空白(或虚拟)图放置在某个地方(例如,靠近uicontrols的定义)。

然后在回调函数中获取当前轴对象:

axes1=gca();// get the current axes

假设您仅绘制了一条线,则折线由axes1.children.children处理(如果您绘制多条线,则它们由axes1.children.children(1),axes1.children.children(2)和等等)。

x和y数据位于折线的“数据”中,因此您只需要更新此数据即可:

axes1.children.children.data=xymatrix([x1,y1;x2,y2;x3,y3;...xn,yn]);

您的回调函数可能如下所示:

function slider_callback(handles)
//Write your callback for  slider  here
x=zeros(101,2);//define matrix with 101 rows and 2 columns
x(:,1)=0:0.1:10;//fill first column with x axis values
a=handles.slider.Value;//get slider value
x(:,2)=a*sin(x(:,1));//fill second column with y axis values
axes1=gca();//get current axes
axes1.children.children.data=x;//update polyline data with new xy data
endfunction