如何从回调函数中获取先前的值?

时间:2010-05-30 15:46:07

标签: user-interface matlab matlab-guide

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

  1. 文本框包含有效字符串
  2. 用户输入无效字符串
  3. 回调函数,验证输入并实现新输入是一个错误,并恢复到之前的旧值。
  4. 应如何实施或完成? 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
    

2 个答案:

答案 0 :(得分:2)

只需在句柄结构中添加一个新字段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

答案 1 :(得分: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。