我知道这可能是一个简单的问题,但我是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
3条答案
按热度按时间7vux5j2d1#
只需在handles结构中添加一个新字段
sampledistPrev
。在GUI的
openingFcn
中,使用如下行定义属性:然后像这样更新回调:
gstyhher2#
为什么不将“先前的值”存储为该对象的“UserData”,如下所示:
% Y.T.
ipakzgxi3#
作为对这个旧线程的贡献,我修改了最新的脚本(asnwer 2),使其成为一个通用脚本,这样你就可以在任何编辑文本框的回调中使用它。脚本自动检测对象的标记。
通过这种方式,您可以通过直接从uicontrol回调调用此脚本来直接调用此脚本进行数值数据验证。
希望有帮助。
最好的问候。