登录论坛

查看完整版本 : 如何获取从回调函数输入的先前值?


poster
2019-12-10, 20:41
我知道这可能是一个简单的问题,但是我对Matlab GUI还是陌生的,基本上是想获取以前存储在文本框中的旧值来替换刚刚输入的值。例如


文本框包含有效字符串,
用户输入了无效的字符串,
回调函数,验证输入并意识到新输入是错误,并恢复为以前的旧值。
应该如何实施或完成? 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

更多&回答... (https://stackoverflow.com/questions/2939285)