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);
4

1 回答 1

1

在您的函数cback中,您已经将变量tmp定义为值为 1 到 4 的标量。然后您立即尝试使用 向others其中添加一个字段tmp.others = ...。这解释了您的错误:

Field assignment to a non-structure array object.

如果tmp已初始化为标量双精度变量,则不能只为其分配结构字段。

无论如何,您没有真正的理由这样做,因为您可以创建另一个变量。只需将您的顶行更改为:

function cback(~,~,editIndex)
    switch editIndex
        case 1
            ...
于 2017-09-22T02:42:53.170 回答