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

j2cgzkjk  于 7个月前  发布在  Matlab
关注(0)|答案(3)|浏览(128)

我知道这可能是一个简单的问题,但我是Matlab GUI的新手,基本上想获取用于存储在文本框中的旧值来替换刚刚输入的值。例如
1.文本框包含有效字符串,
1.用户输入无效字符串,
1.回调函数,验证输入并意识到新输入是错误的,并恢复到旧的先前值。
这应该如何实施或完成?我只是使用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
7vux5j2d

7vux5j2d1#

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

gstyhher2#

为什么不将“先前的值”存储为该对象的“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.

ipakzgxi

ipakzgxi3#

作为对这个旧线程的贡献,我修改了最新的脚本(asnwer 2),使其成为一个通用脚本,这样你就可以在任何编辑文本框的回调中使用它。脚本自动检测对象的标记。
通过这种方式,您可以通过直接从uicontrol回调调用此脚本来直接调用此脚本进行数值数据验证。
希望有帮助。
最好的问候。

function Edit_Box_Numeric_Value_Check(hObject, handles)

str = str2double(get(hObject,'String'));
if (isempty(str)||isnan(str))
    warndlg('Input must be a real number','Warning');
    % errordlg('Input must be a real number','Warning');
    val=str2double(get(hObject,'UserData'));
    if (isempty(val)||isnan(val))
        val='';
    end
    set(hObject,'String',val); %<--- This is where you'd like to set the previous entry value!
    guidata(hObject,handles);
else
    str=num2str(str);
    set(eval(sprintf('handles.%s',hObject.Tag)),'String',str,'UserData',str);
    guidata(hObject,handles);
end

相关问题