2010-05-30 22 views
3

我知道這可能是一個簡單的問題,但我是Matlab GUI的新手,基本上想要獲得原來存儲在文本框中的值,以替換剛剛輸入的值。例如。如何獲得從回調函數中輸入的前一個值?

  1. 文本框中包含一個有效的字符串,
  2. 用戶輸入無效的字符串,
  3. 回調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 

回答

2

只是一個新的領域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 
+1

這是在matlab中做這種事情的唯一方法嗎?看起來有點醜,只是爲了擁有一個龐大的結構,將一切都放在一個大的結構中。 – binarycreations 2010-05-30 18:46:39

+0

或者,您可以使用'setappdata' /'getappdata',或者您可以定義一個'persistent'變量,即只存在於'sampledist_Callback'中的全局變量。我發現「手柄」方法最簡單,我從來沒有遇到太大的結構問題。 – Jonas 2010-05-30 18:56:12

+0

@格拉漢姆 - 是的,它很醜。不,它不是唯一的方法,但它正是MATLAB設計者希望你做的事情。 Matlab有很多東西(沒有辦法強制變量的輸入,僅傳值的規則,改變調用函數中被調用函數的值的能力)構建「真實」編程語言以消除或阻止。但是一旦你意識到這一點,你有時只需要滾動它。 – Marc 2010-05-30 20:43:57

2

你爲什麼不保存「的先前值「作爲該對象的'用戶數據',如下:


function sampledist_Callback(hObject, eventdata, handles) 
    input = str2double(get(hObject,'String')); 
    if (input < 0 || input > 500) 
     errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error'); 
     val=get(hObject,'UserData'); 
     if isempty(val) 
      val=''; 
     end 
     set(hObject,'String',val); %<--- This is where you'd like to set the previous entry value! 
     guidata(hObject,handles); 
    else 
     input=num2str(input); 
     set(handles.sampledist,'String',input,'UserData',input); 
     guidata(hObject,handles); 
    end 
end 

%Y.T.

+0

我更喜歡這個版本! – 2014-04-30 05:35:36

相關問題