本文介绍了MATLAB GUI错误引用不存在的字段'---'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个edittext框。我想在用户单击其他位置而不是这些edittext框时给出错误消息。我收到消息

I have two edittext boxes. I want to give an error message as soon as user clicks another place rather than these edittext boxes. I receive message

事实上,在这两个之前我有几个edittext框。当我打印 S 时,我可以看到字段名打印到 xegim 但不是 xegim

In fact, I have several edittext boxes before these two. When I print S, I can see the fieldnames are printed up to xegim but not xegim

S.fh = figure('Visible','on','numbertitle','off','Name','GUI',...
    'units','pixels','Position',[50 50 1500 750]);
% Panel start
S.ph_arazi = uipanel('Parent',S.fh,'Title','Arazi','TitlePosition','centertop',...
    'units','pixels','Position',[1180 364 300 244]);

S.sth_xegim = uicontrol(S.ph_arazi,'Style','text','String','x Eksenine gore egim [%]','HorizontalAlignment','left','Position',[10 68 130 18]);
% xegim edittext
S.eth_xegim = uicontrol(S.ph_arazi,'Style','edit','String','0',...
    'HorizontalAlignment','right','BackgroundColor','w',...
    'Position',[250 68 40 18],'callback',{@errmsg,S});

S.sth_yegim = uicontrol(S.ph_arazi,'Style','text',...
    'String','y Eksenine gore egim [%]','HorizontalAlignment','left',...
    'Position',[10 40 130 18]);
% xegim edittext
S.eth_yegim = uicontrol(S.ph_arazi,'Style','edit',...
    'String','0','HorizontalAlignment','right',...
    'BackgroundColor','w','Position',[250 40 40 18],...
    'callback',{@errmsg,S});
% error message
function [] = errmsg(varargin)
S = varargin{3}

xegim = get(S.eth_xegim,'String');
xegim = xegim(ismember(xegim,'.:0123456789'));
yegim = get(S.eth_yegim,'String');
yegim = yegim(ismember(yegim,'.:0123456789'));
if isempty(xegim) || isempty(yegim)
    if length(S.fh)==1
        S.fh(2) = figure('numbertitle','off','Name','Error',...
            'menubar','none','units','pixels',...
            'Position',[700 400 200 100]);

        S.sth_error = uicontrol(S.fh(2),'Style','text',...
            'String','Girdiler sayi olmali!','Min',0,'Max',2,...
            'BackgroundColor','r','Position',[10 10 180 80]);
        ag = findobj;
        nf = max(ag(find(ag==fix(ag))));
        if nf == 2
        close(S.fh(2))
        end
        S.fh(2) = [];
    end
    return;
end


推荐答案

当你添加 S 作为附加输入参数的回调 S 不包含字段 eth_xegim eth_yegim 因为它们尚未添加到结构中。在添加字段后定义回调:

At the time you add S to the callback as an additional input argument S does not contain the fields eth_xegim and eth_yegim as they have not been added to the structure. Define the callbacks after fields have been added:

S.eth_xegim = uicontrol(S.ph_arazi,'Style','edit','String','0',...
    'HorizontalAlignment','right','BackgroundColor','w',...
    'Position',[250 68 40 18]);

S.eth_yegim = uicontrol(S.ph_arazi,'Style','edit',...
    'String','0','HorizontalAlignment','right',...
    'BackgroundColor','w','Position',[250 40 40 18]);


S.eth_xegim.Callback = {@errmsg,S};
S.eth_yegim.Callback = {@errmsg,S};

这篇关于MATLAB GUI错误引用不存在的字段'---'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 04:28