Come ottenere il valore precedente immesso da una funzione di callback?
-
05-10-2019 - |
Domanda
So che questo è probabilmente un problema semplice, ma io sono nuovo di Matlab GUI e fondamentalmente vogliono ottenere il valore storico che ha usato per essere memorizzati nella casella di testo per sostituire il valore che è stato appena inserito. Per es.
- casella di testo contiene una stringa valida,
- entra utente non valido stringa,
- Callback func, convalida di ingresso e realizza nuovi input è un errore e ripristina il vecchio valore precedente.
Come dovrebbe essere implementato questo o fatto? Atm sto solo usando i valori delle proprietà get e set. Di seguito è riportato un codice di esempio:
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
Soluzione
È sufficiente aggiungere un nuovo sampledistPrev
campo per la vostra struttura maniglie.
Nel openingFcn
della GUI, definire la proprietà con una linea come questa:
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)
quindi si aggiorna il callback in questo modo:
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
Altri suggerimenti
Perché non memorizzare "il valore precedente", come il 'UserData' di tale oggetto, come segue:
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.