You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab 2012中'Attempt to reference field of non-structure array'错误排查求助

Matlab 2012中单选按钮组回调函数报错分析

问题场景

开发了一个Matlab程序,包含两组单选按钮(radiobutton),每个单选按钮对应一个编辑框(edit box)。需求为:选中单选按钮时(不同组可同时选中2个),启用其对应的编辑框,未选中按钮的编辑框均禁用。

程序代码

function fig=exemple1
    fig = figure('Units','normalized');
    uibgA = uibuttongroup(fig,'Position',[0,0.5,0.5,0.5],'Title','ButtonGroupA')
    uibgB = uibuttongroup(fig,'Position',[0,0.0,0.5,0.5],'Title','ButtonGroupB')
    uipA = uipanel(fig,'Position',[0.5,0.5,0.5,0.5],'Title','Group A')
    uipB = uipanel(fig,'Position',[0.5,0.0,0.5,0.5],'Title','Group B')
    grpARadioLabels = {
        'Choice 1','Tooltip 1'
        'Choice 2','Tooltip 2'
        'Choice 3','Tooltip 3'
        };
    grpBRadioLabels = {
        'Choice 1','Tooltip 1'
        'Choice 2','Tooltip 2'
        'Choice 3','Tooltip 3'
        'Choice 4','Tooltip 4'
        };
    for i = 3:-1:1
        rbA(i) = uicontrol(uibgA,'Style','radiobutton',...
            'String',grpARadioLabels{i,1},...
            'TooltipString',grpARadioLabels{i,2},...
            'Position',[10,10+i*22,200,22] ...
            )
        
        prmsA{i} = uicontrol(uipA,'Style','edit',...
            'Position',[10,10+i*22,100,22],...
            'Tag',grpARadioLabels{i,1},...
            'Enable','off',...
            'String',sprintf('param %d',i))
    end
    for i = 4:-1:1
        rbB(i) = uicontrol(uibgB,'Style','radiobutton',...
            'String',grpBRadioLabels{i,1},...
            'TooltipString',grpBRadioLabels{i,2},...
            'Position',[10,10+i*22,200,22] ...
            )
        prmsB{i} = uicontrol(uipB,'Style','edit',...
            'Position',[10,10+i*22,100,22],...
            'Tag',grpBRadioLabels{i,1},...
            'Enable','off',...
            'String',sprintf('param %d',i))
    end
set(uibgA,'SelectionChangeFcn',@(o,e)bgChangeFcn(o,e,prmsA))
set(uibgB,'SelectionChangeFcn',@(o,e)bgChangeFcn(o,e,prmsB))
    end
    
    function bgChangeFcn(src,event,prms)
    allprms = [prms{:}];
    set(allprms,'Enable','off');
    lbl = event.NewValue.String;
    set(findobj(allprms,'Tag',lbl),'Enable','on');
    end

运行报错信息

Attempt to reference field of non-structure array.

Error in exemple1>bgChangeFcn (line 56)
lbl = event.NewValue.String;

Error in exemple1>@(o,e)bgChangeFcn(o,e,prmsA) (line 49)
set(uibgA,'SelectionChangeFcn',@(o,e)bgChangeFcn(o,e,prmsA))

Error in hgfeval (line 63)
        feval(fcn{1},varargin{:},fcn{2:end});

Error in uitools.uibuttongroup/childAddedCbk>manageButtons (line 80)
    hgfeval(cbk, source, evdata);
 
Error while evaluating uicontrol Callback

Attempt to reference field of non-structure array.

错误原因分析

这个错误的核心是Matlab版本兼容性差异:

  • Matlab R2014b及之后的版本采用HG2图形系统,uibuttongroup的SelectionChangeFcn回调参数event是结构体,包含NewValue字段(指向选中的单选按钮对象),可以通过event.NewValue.String获取按钮文本。
  • 但你使用的Matlab 2012属于HG1图形系统,SelectionChangeFcn的第二个参数直接是选中的单选按钮句柄,并非结构体,不存在NewValue字段。代码中lbl = event.NewValue.String;试图引用非结构体的字段,因此触发报错。

内容的提问来源于stack exchange,提问作者ali herssi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 04:35:40