我的错误是
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);
答案 0 :(得分:1)
在您的函数cback
中,您已将变量tmp
定义为值为1到4的标量。然后您立即尝试向其中添加字段others
tmp.others = ...
。这解释了您的错误:
Field assignment to a non-structure array object.
如果tmp
已初始化为标量双变量,则不能只为其分配结构字段。
无论如何你都没有真正的理由,因为你可以制作另一个变量。只需将您的顶行更改为:
function cback(~,~,editIndex)
switch editIndex
case 1
...