gpt4 book ai didi

user-interface - 如何从回调函数中获取之前输入的值?

转载 作者:太空宇宙 更新时间:2023-11-03 20:26:56 24 4
gpt4 key购买 nike

我知道这可能是一个简单的问题,但我是 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

最佳答案

只需将新字段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

关于user-interface - 如何从回调函数中获取之前输入的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2939285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com