gui回调matlab的嵌套函数问题
issue with nested functions for gui callback matlab
我的错误是
Field assignment to a non-structure array object.
Error in showTF/cback (line 481)
tmp.others = get(gcbo,'UserData');
Error while evaluating UIControl Callback
我正在尝试将字符向量回调转换为函数句柄。我尝试使用元胞数组来接受每个案例条件的额外输入。
我认为这里的主要问题是语法和组件之间数据共享的一些知识。
这里是旧代码:
cback = ['switch tmp.m, '...
'case 1;'...
'tmp.others = get(gcbo,''UserData'');'...
'tmp.n1 = str2num(get(gcbo,''String''));'...
'tmp.n2 = str2num(get(tmp.others(1),''String''));'...
'tmp.J = str2num(get(tmp.others(2),''String''));'...
'tmp.Jm = str2num(get(tmp.others(3),''String''));'...
'case 2;'...
'tmp.others = get(gcbo,''UserData'');'...
'tmp.n2 = str2num(get(gcbo,''String''));'...
'tmp.n1 = str2num(get(tmp.others(1),''String''));'...
'tmp.J = str2num(get(tmp.others(2),''String''));'...
'tmp.Jm = str2num(get(tmp.others(3),''String''));'...
'case 3;'...
'tmp.others = get(gcbo,''UserData'');'...
'tmp.J = str2num(get(gcbo,''String''));'...
'tmp.n2 = str2num(get(tmp.others(1),''String''));'...
'tmp.n1 = str2num(get(tmp.others(2),''String''));'...
'tmp.Jm = str2num(get(tmp.others(3),''String''));'...
'case 4;'...
'tmp.others = get(gcbo,''UserData'');'...
'tmp.Jm = str2num(get(gcbo,''String''));'...
'tmp.n1 = str2num(get(tmp.others(1),''String''));'...
'tmp.n2 = str2num(get(tmp.others(2),''String''));'...
'tmp.J = str2num(get(tmp.others(3),''String''));'...
'end;'...
'tmp.str = [''$J=\frac{N_1^2}{N_2^2}\cdot J_{load}+J_m='' num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm) ''kg \cdot m^2$'']; '...
'set(result ,''String'',tmp.str); set(button,''Enable'',''on'',''UserData'',num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm)); clear tmp'];
我的每个条件的 uicontrol 回调
edit(1) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 80 40 20],'String','1','Style','edit','Callback',['tmp.m=1; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(2) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 60 40 20],'String','1','Style','edit','Callback',['tmp.m=2; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(3) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 40 40 20],'String','0','Style','edit','Callback',['tmp.m=3; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(4) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 20 40 20],'String','0.0019','Style','edit','Callback',['tmp.m=4; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
正在尝试编写嵌套函数:
function cback(~,~,tmp)
switch tmp
case 1
tmp.others = get(gcbo,'UserData');
tmp.n1 = str2num(get(gcbo,'String'));
tmp.n2 = str2num(get(tmp.others(1),'String'));
tmp.J = str2num(get(tmp.others(2),'String'));
tmp.Jm = str2num(get(tmp.others(3),'String'));
case 2
tmp.others = get(gcbo,'UserData');
tmp.n2 = str2num(get(gcbo,'String'));
tmp.n1 = str2num(get(tmp.others(1),'String'));
tmp.J = str2num(get(tmp.others(2),'String'));
tmp.Jm = str2num(get(tmp.others(3),'String'));
case 3
tmp.others = get(gcbo,'UserData');
tmp.J = str2num(get(gcbo,'String'));
tmp.n2 = str2num(get(tmp.others(1),'String'));
tmp.n1 = str2num(get(tmp.others(2),'String'));
tmp.Jm = str2num(get(tmp.others(3),'String'));
case 4
tmp.others = get(gcbo,'UserData');
tmp.Jm = str2num(get(gcbo,'String'));
tmp.n1 = str2num(get(tmp.others(1),'String'));
tmp.n2 = str2num(get(tmp.others(2),'String'));
tmp.J = str2num(get(tmp.others(3),'String'));
end
tmp.str = ['$J=\frac{N_1^2}{N_2^2}\cdot J_{load}+J_m=' num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm) 'kg \cdot m^2$'];
set(result ,'String',tmp.str);set(button,'Enable','on','UserData',num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm)); clear tmp;
end
我的每个条件的 uicontrol 回调
edit(1) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 80 40 20],'String','1','Style','edit','Callback',{@cback,1},'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(2) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 60 40 20],'String','1','Style','edit','Callback',{@cback,2},'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(3) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 40 40 20],'String','0','Style','edit','Callback',{@cback,3},'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(4) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 20 40 20],'String','0.0019','Style','edit','Callback',{@cback,4},'BackgroundColor',[1 1 1],'ToolTipString',tt);
在您的函数 cback
中,您已经将变量 tmp
定义为一个值为 1 到 4 的标量。然后您立即尝试将一个字段 others
添加到它与 tmp.others = ...
。这解释了你的错误:
Field assignment to a non-structure array object.
如果 tmp
已经初始化为标量双精度变量,则不能只为其分配结构字段。
无论如何你都没有真正的理由这样做,因为你可以创建另一个变量。只需将顶行更改为:
function cback(~,~,editIndex)
switch editIndex
case 1
...
我的错误是
Field assignment to a non-structure array object.
Error in showTF/cback (line 481)
tmp.others = get(gcbo,'UserData');
Error while evaluating UIControl Callback
我正在尝试将字符向量回调转换为函数句柄。我尝试使用元胞数组来接受每个案例条件的额外输入。 我认为这里的主要问题是语法和组件之间数据共享的一些知识。
这里是旧代码:
cback = ['switch tmp.m, '...
'case 1;'...
'tmp.others = get(gcbo,''UserData'');'...
'tmp.n1 = str2num(get(gcbo,''String''));'...
'tmp.n2 = str2num(get(tmp.others(1),''String''));'...
'tmp.J = str2num(get(tmp.others(2),''String''));'...
'tmp.Jm = str2num(get(tmp.others(3),''String''));'...
'case 2;'...
'tmp.others = get(gcbo,''UserData'');'...
'tmp.n2 = str2num(get(gcbo,''String''));'...
'tmp.n1 = str2num(get(tmp.others(1),''String''));'...
'tmp.J = str2num(get(tmp.others(2),''String''));'...
'tmp.Jm = str2num(get(tmp.others(3),''String''));'...
'case 3;'...
'tmp.others = get(gcbo,''UserData'');'...
'tmp.J = str2num(get(gcbo,''String''));'...
'tmp.n2 = str2num(get(tmp.others(1),''String''));'...
'tmp.n1 = str2num(get(tmp.others(2),''String''));'...
'tmp.Jm = str2num(get(tmp.others(3),''String''));'...
'case 4;'...
'tmp.others = get(gcbo,''UserData'');'...
'tmp.Jm = str2num(get(gcbo,''String''));'...
'tmp.n1 = str2num(get(tmp.others(1),''String''));'...
'tmp.n2 = str2num(get(tmp.others(2),''String''));'...
'tmp.J = str2num(get(tmp.others(3),''String''));'...
'end;'...
'tmp.str = [''$J=\frac{N_1^2}{N_2^2}\cdot J_{load}+J_m='' num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm) ''kg \cdot m^2$'']; '...
'set(result ,''String'',tmp.str); set(button,''Enable'',''on'',''UserData'',num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm)); clear tmp'];
我的每个条件的 uicontrol 回调
edit(1) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 80 40 20],'String','1','Style','edit','Callback',['tmp.m=1; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(2) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 60 40 20],'String','1','Style','edit','Callback',['tmp.m=2; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(3) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 40 40 20],'String','0','Style','edit','Callback',['tmp.m=3; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(4) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 20 40 20],'String','0.0019','Style','edit','Callback',['tmp.m=4; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
正在尝试编写嵌套函数:
function cback(~,~,tmp)
switch tmp
case 1
tmp.others = get(gcbo,'UserData');
tmp.n1 = str2num(get(gcbo,'String'));
tmp.n2 = str2num(get(tmp.others(1),'String'));
tmp.J = str2num(get(tmp.others(2),'String'));
tmp.Jm = str2num(get(tmp.others(3),'String'));
case 2
tmp.others = get(gcbo,'UserData');
tmp.n2 = str2num(get(gcbo,'String'));
tmp.n1 = str2num(get(tmp.others(1),'String'));
tmp.J = str2num(get(tmp.others(2),'String'));
tmp.Jm = str2num(get(tmp.others(3),'String'));
case 3
tmp.others = get(gcbo,'UserData');
tmp.J = str2num(get(gcbo,'String'));
tmp.n2 = str2num(get(tmp.others(1),'String'));
tmp.n1 = str2num(get(tmp.others(2),'String'));
tmp.Jm = str2num(get(tmp.others(3),'String'));
case 4
tmp.others = get(gcbo,'UserData');
tmp.Jm = str2num(get(gcbo,'String'));
tmp.n1 = str2num(get(tmp.others(1),'String'));
tmp.n2 = str2num(get(tmp.others(2),'String'));
tmp.J = str2num(get(tmp.others(3),'String'));
end
tmp.str = ['$J=\frac{N_1^2}{N_2^2}\cdot J_{load}+J_m=' num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm) 'kg \cdot m^2$'];
set(result ,'String',tmp.str);set(button,'Enable','on','UserData',num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm)); clear tmp;
end
我的每个条件的 uicontrol 回调
edit(1) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 80 40 20],'String','1','Style','edit','Callback',{@cback,1},'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(2) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 60 40 20],'String','1','Style','edit','Callback',{@cback,2},'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(3) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 40 40 20],'String','0','Style','edit','Callback',{@cback,3},'BackgroundColor',[1 1 1],'ToolTipString',tt);
edit(4) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 20 40 20],'String','0.0019','Style','edit','Callback',{@cback,4},'BackgroundColor',[1 1 1],'ToolTipString',tt);
在您的函数 cback
中,您已经将变量 tmp
定义为一个值为 1 到 4 的标量。然后您立即尝试将一个字段 others
添加到它与 tmp.others = ...
。这解释了你的错误:
Field assignment to a non-structure array object.
如果 tmp
已经初始化为标量双精度变量,则不能只为其分配结构字段。
无论如何你都没有真正的理由这样做,因为你可以创建另一个变量。只需将顶行更改为:
function cback(~,~,editIndex)
switch editIndex
case 1
...