【问题标题】:Scilab - gui - many unknown variables error messagesScilab - gui - 许多未知变量错误消息
【发布时间】:2018-05-25 02:31:50
【问题描述】:

仍然试图了解 Scilab 的逻辑,我为机械元件创建了一个小型计算工具。我遇到的主要问题是为计算代码找到正确的顺序(或语法)......我收到很多“未知变量”错误,我不明白为什么? 我试图更改函数的定义顺序,将变量声明为全局变量等,但似乎没有任何帮助。 计算的代码不长也不复杂,但是 gui 是使用 guibuilder 构建的,因此 uicontrols 定义可能比需要的长得多。 有人可以帮我使这段代码正常工作吗,因为我会通过这个例子学到很多东西,尽管它包含多个“问题区域”? 这是我所做的:

G = 78500;

table_titles = ["" "Wire diameter" "Wp" "Tau alwd" "M alwd" "Angle alwd"];

f=figure('figure_position',[910,163],'figure_size',
[903,537],'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.sl_dwire=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',[12],'Min',[0],'Position',
[0.0058208,0.77875,0.124375,0.06875],'Relief','default','SliderStep',
[0.1,1],'Style','slider','String',"Wire diameter",'Value',
[6],'VerticalAlignment','middle','Visible','on','Tag','sl_dwire',
'Callback','sl_dwire_callback(handles)')
handles.ed_dwire=uicontrol(f,'unit','normalized','BackgroundColor',
[-1,-1,-1],'Enable','off','FontAngle','normal','FontName','Tahoma',
'FontSize',[12],'FontUnits','points','FontWeight','normal',
'ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left','ListboxTop',
[],'Max',[1],'Min',[0],
'Position',[0.0058208,0.71875,0.124375,0.06875],'Relief',
'default','SliderStep',[0.01,0.1],'String',"wire diameter: " + 
msprintf('%2.1f',handles.sl_dwire.Value) + "mm",'Style','text',
'Value',[0],'VerticalAlignment','middle','Visible','on','Tag',
'ed_dwire','Callback','auto')
handles.sl_wangle=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',[180],'Min',[5],'Position',
[0.0090625,0.5191667,0.25625,0.0645833],'Relief','default',
'SliderStep',[0.1,1],'String','Working angle','Style','slider','Value',
[50],'VerticalAlignment','middle','Visible','on','Tag','sl_wangle',
'Callback','sl_wangle_callback(handles)')
handles.ed_wangle=uicontrol(f,'unit','normalized','BackgroundColor',
[-1,-1,-1],'Enable','off','FontAngle','normal','FontName','Tahoma',
'FontSize',[12],'FontUnits','points','FontWeight','normal',
'ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left',
'ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.0090625,0.4591667,0.25625,0.0645833],'Relief','default',
'SliderStep',[0.01,0.1],'String',"Working angle: " + 
msprintf('%2.1f',handles.sl_wangle.Value) + "°",'Style','text',
'Value',[0],'VerticalAlignment','middle','Visible','on','Tag',
'ed_wangle','Callback','auto')
handles.sl_activel=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',[1000],'Min',[10],'Position',
[0.0090625,0.365,0.25625,0.0645833],'Relief','default',
'SliderStep',[0.1,1],'String','Active length' ,'Style','slider',
'Value',[10],'VerticalAlignment','middle','Visible','on','Tag',
'sl_activel','Callback','sl_activel_callback(handles)')
handles.ed_activel=uicontrol(f,'unit','normalized',
'BackgroundColor',[-1,-1,-1],'Enable','off','FontAngle','normal',
'FontName','Tahoma','FontSize',[12],'FontUnits','points','FontWeight',
'normal','ForegroundColor',[-1,-1,-1],'HorizontalAlignment','left',
'ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.0090625,0.305,0.25625,0.0645833],'Relief','default',
'SliderStep',[0.01,0.1],'String','Active length: ' + 
msprintf('%2.1f',handles.sl_activel.Value) +
"mm",'Style','text','Value',[0],'VerticalAlignment','middle','Visible',
'on','Tag','ed_activel','Callback','auto')
handles.ax_graph= newaxes();handles.ax_graph.margins = [ 0 0 0 0];
handles.ax_graph.axes_bounds = [0.4274266,0.0619266,0.3995485,0.5191743];
handles.tab_param=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.4308126,0.1690826,0.3950339,0.2178899],'Relief',
'default','SliderStep',[0.01,0.1],'String',string(table_param),'Style',
'table','Value',[0],'VerticalAlignment','middle','Visible',
'on','Tag','tab_param','Callback','tab_param_callback(handles)')
handles.sl_sfactor=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.0058208,0.6525688,0.124375,0.06875],'Relief','default',
'SliderStep',[0.01,0.1],'String',"Safety factor",'Style',
'slider','Value',[0.58],'VerticalAlignment','middle','Visible','on',
'Tag','ed_sfactor','Callback','sl_sfactor_callback(handles)')
handles.ed_sfactor=uicontrol(f,'unit','normalized',
'BackgroundColor',[-1,-1,-1],'Enable','off','FontAngle','normal',
'FontName','Tahoma','FontSize',[12],'FontUnits','points','FontWeight',
'normal','ForegroundColor',[-1,-1,-1],'HorizontalAlignment',
'left','ListboxTop',[],'Max',[1],'Min',[0],'Position',
[0.0058208,0.5925688,0.124375,0.06875],'Relief','default',
'SliderStep',[0.01,0.1],'String',"Safety factor : " + 
msprintf('%2.1f',handles.sl_sfactor.Value),'Style',
'text','Value',[0.58],'VerticalAlignment','middle','Visible','on',
'Tag','ed_dwire','Callback','auto')
handles.popm_wtype=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.0058208,0.8618349,0.124375,0.0639450],'Relief','default',
'SliderStep',[0.01,0.1],'String',gettext("SL/DL|SM/DM|SH/DH"),'Style',
'popupmenu','Value',[2],'VerticalAlignment','middle','Visible','on',
'Tag','popm_wtype','Callback','popm_wtype_callback(handles)')

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

function sl_sfactor_callback(handles)
   sf=handles.sl_sfactor.Value; 
   handles.ed_sfactor.String="Safety factor: " + msprintf('%3.2f',sf);
endfunction

function sl_dwire_callback(handles)
   wd=handles.sl_dwire.Value;
   Wp = %pi*wd^3/16;
   Ip =%pi*wd^4/32;
   Kt = G*%pi*Ip/(180*L);
   Talwd = sf * calcform;
   Malwd = Wp * Talwd;
   alphaalwd = Malwd / Kt;
   x=0:0.1:alphaalwd*1.5;
   plot(x,Kt*x);
   handles.ed_dwire.String="Wire diameter: " + 
   msprintf('%2.1f',wd) + "mm";
endfunction

function popm_wtype_callback(handles)
//Write your callback for  popm_wtype  here
     if selected == 1 then
     calcform =(1845 - 700*log10(wd));
 elseif selected == 2  then
     calcform =(2105 - 780*log10(wd));
 elseif selected == 3 then
     calcform = (2220 - 820*log10(wd));
 end
endfunction

function sl_wangle_callback(handles)
    handles.ed_wangle.String="Working angle: " + 
    msprintf('%2.1f',handles.sl_wangle.Value) + "°";   
endfunction

function sl_activel_callback(handles)
//Write your callback for  sl_activel  here
    handles.ed_activel.String="Active length: " + 
    msprintf('%2.1f',handles.sl_activel.Value) + "mm";
    L=handles.sl_activel.Value;
endfunction

function tab_param_callback(handles)
//Write your callback for  tab_param  here
   table_values = string([ wd Wp Talwd Malwd alphaalwd]);
   table_param = [table_titles; [table_values]];
endfunction

我希望代码能够根据输入滑块和弹出菜单的位置动态更新图形和参数表。

同样,如果有人可以帮助我使这段代码正常工作,那将非常有帮助,因为我会得到很多关于使用 scilab 编程的问题的答案。

非常感谢您!

【问题讨论】:

  • 您的问题似乎是您不了解 Scilab 中范围的限制。您在函数内部创建变量并尝试从该函数外部调用它们。这是不允许的。
  • 我试着理解你的代码,但它太复杂了。你能用guibuilder做一个非常简单的例子吗?也许只有一个滑块,或者一个方程?它必须是具有相同问题的“未定义变量”。这就是我们所说的minimal reproducible example

标签: scilab


【解决方案1】:

首先,由于换行符,您的代码不能直接执行。请在声明的每一行后添加...。这也提高了可读性。 正如@luispauloml 所提到的,您尝试使用仅存在于另一个函数内部的变量。例如,您尝试访问table_param,它在tab_param_callback(handles) 中仍然存在。 为了摆脱这个,你必须定义函数的输出:

函数 [table_values, table_param] = tab_param_callback(handles)

table_values = string([ wd Wp Talwd Malwd alphaalwd]);

table_param = [table_titles; [table_values]];

结束功能

现在你可以调用这个函数来获取变量了:

字符串(tab_param_callback(句柄))

我纠正了这种情况并按照说明添加了点。对于其他变量,可以类推。此外,我将函数定义移到了代码的开头。因为如果你的脚本在中间崩溃了,编译器就没有机会读取函数定义了。

请在this file找到代码。

我希望这会有所帮助。祝你好运!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 2021-09-03
  • 2020-11-02
  • 2020-09-08
相关资源
最近更新 更多