查看单个帖子
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何获取从回调函数输入的先前值?

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

更多&回答...
poster 当前离线   回复时引用此帖