【问题标题】:How to get the previous value entered from a callback function?如何获取从回调函数输入的先前值?
【发布时间】:2010-05-30 15:46:07
【问题描述】:

我知道这可能是一个简单的问题,但我是 Matlab GUI 的新手,基本上想获取以前存储在文本框中的旧值来替换刚刚输入的值。例如

  1. 文本框包含有效字符串,
  2. 用户输入了无效的字符串,
  3. 回调函数,验证输入并意识到新输入是错误的并恢复为旧的先前值。

这应该如何实施或完成? Atm 我只是使用 get 和 set 属性值。 下面是一些示例代码:

function sampledist_Callback(hObject, eventdata, handles)
% hObject    handle to sampledist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of sampledist as text
%        str2double(get(hObject,'String')) returns contents of sampledist as a double

input = str2double(get(hObject,'String'));
if(input < 0 || input > 500)
    errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error');
    set(handles.sampledist,'String',['10']); %<--- I would like this value 10 to be the previous entry!
    guidata(hObject,handles);
else
   set(handles.sampledist,'String',['',input]);
   guidata(hObject,handles);
end

【问题讨论】:

    标签: user-interface matlab matlab-guide


    【解决方案1】:

    只需在句柄结构中添加一个新字段 sampledistPrev

    在 GUI 的 openingFcn 中,使用如下行定义属性:

    handles.sampledistPrev = 10; %# or whatever you choose as default value
    %# if you want, you can set the default value to the GUI, so that you only need 
    %# to change it at one point, if necessary, like so:
    set(handles.sampledist,'String',num2str(handles.sampledistPrev));
    %# don't forget to save the handles structure at the end of the openingFcn
    guidata(hObject,handles)
    

    然后你像这样更新你的回调:

    function sampledist_Callback(hObject, eventdata, handles)
    % hObject    handle to sampledist (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of sampledist as text
    %        str2double(get(hObject,'String')) returns contents of sampledist as a double
    
    input = str2double(get(hObject,'String'));
    if(input < 0 || input > 500)
        errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error');
        set(handles.sampledist,'String',num2str(handles.sampledistPrev)); %reset value be the previous entry!
        guidata(hObject,handles); %# Note that you don't need to save the handles structure unless
                                  %# you have changed a user-defined value like sampledistPrev
                                  %# It may still be useful to do it so you always remember
    else
       set(handles.sampledist,'String',['',input]);
       %# also update the reset value
       handles.sampledistPrev = input;
       guidata(hObject,handles);
    end
    

    【讨论】:

    • 这是在matlab中做这种事情的唯一方法吗?一个庞大的结构将所有东西都放在一个大结构中似乎有点丑陋。
    • 或者,您可以使用setappdata/getappdata,或者您可以定义一个persistent 变量,即一种仅存在于sampledist_Callback 中的全局变量。我发现handles 方法最简单,而且我从来没有遇到过结构过于庞大的问题。
    • @Graham - 是的,它很丑。不,这不是唯一的方法,但它是 MATLAB 设计人员希望您做事的方式。 Matlab 有很多东西(无法强制键入变量、仅按值传递规则、从被调用函数更改调用函数中的值的能力),“真正的”编程语言旨在消除或阻止这些东西。但是一旦你意识到这一点,你有时就不得不顺其自然。
    • @Marc + @Jonas,嗯,我只是将字段添加到结构中。顺便说一句,在编程语言注释上,我喜欢易于绘图的 matlabs,具有出色和有用功能的庞大库以及将图像等简单表示为矩阵,但我错过了更多 OO 语言(例如 C++、Java)提供的所有规则和结构我一直在长大。是否有任何语言可以封装两者?
    • @Graham:自 r2008a 以来,Matlab 实际上已经实现了 OOP - 例如,它现在也允许按值传递,例如。如果你想要像 Matlab 这样更像一种编程语言的东西,Python 可能是一种可能——尽管它以及许多其他环境缺乏我最喜欢的可能是 Matlab 的东西:优秀的文档和高水平的向后兼容性。跨度>
    【解决方案2】:

    为什么不将“前一个值”存储为该对象的“UserData”,如下所示:


    function sampledist_Callback(hObject, eventdata, handles)
        input = str2double(get(hObject,'String'));
        if (input < 0 || input > 500)
            errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error');
            val=get(hObject,'UserData');
            if isempty(val)
                val='';
            end
            set(hObject,'String',val); %<--- This is where you'd like to set the previous entry value!
            guidata(hObject,handles);
        else
            input=num2str(input);
            set(handles.sampledist,'String',input,'UserData',input);
            guidata(hObject,handles);
        end
    end
    

    % Y.T.

    【讨论】:

    • 我更喜欢这个版本!
    猜你喜欢
    • 2021-09-16
    • 2013-04-30
    • 2017-02-09
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多