我知道這可能是一個簡單的問題,但我是Matlab GUI的新手,基本上想要獲得原來存儲在文本框中的值,以替換剛剛輸入的值。例如。如何獲得從回調函數中輸入的前一個值?
- 文本框中包含一個有效的字符串,
- 用戶輸入無效的字符串,
- 回調FUNC,驗證輸入,實現了新的輸入錯誤並且恢復到舊的前值。
這應該如何實施或完成? 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
這是在matlab中做這種事情的唯一方法嗎?看起來有點醜,只是爲了擁有一個龐大的結構,將一切都放在一個大的結構中。 – binarycreations 2010-05-30 18:46:39
或者,您可以使用'setappdata' /'getappdata',或者您可以定義一個'persistent'變量,即只存在於'sampledist_Callback'中的全局變量。我發現「手柄」方法最簡單,我從來沒有遇到太大的結構問題。 – Jonas 2010-05-30 18:56:12
@格拉漢姆 - 是的,它很醜。不,它不是唯一的方法,但它正是MATLAB設計者希望你做的事情。 Matlab有很多東西(沒有辦法強制變量的輸入,僅傳值的規則,改變調用函數中被調用函數的值的能力)構建「真實」編程語言以消除或阻止。但是一旦你意識到這一點,你有時只需要滾動它。 – Marc 2010-05-30 20:43:57