¿Cómo obtener el valor anterior introducida desde una función de devolución de llamada?

StackOverflow https://stackoverflow.com/questions/2939285

Pregunta

Yo sé que esto es probablemente un problema sencillo pero soy nuevo en Matlab GUI y básicamente quieren obtener el valor antiguo que solía ser almacenados en el cuadro de texto para reemplazar el valor que acaba de ser introducido. Por ejemplo.

  1. contiene cuadro de texto una cadena válida,
  2. entra usuario no válido cadena,
  3. Devolución de llamada func, valida la entrada y se da cuenta nueva entrada es un error y vuelve a la vieja valor anterior.

¿Cómo debe ser implementado o se hace? Atm sólo estoy usando los valores de las propiedades Get y Set. A continuación se muestra un código de ejemplo:

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
¿Fue útil?

Solución

Basta con añadir una nueva sampledistPrev campo a su estructura de asas.

En el openingFcn de la GUI, definir la propiedad con una línea como la siguiente:

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)

A continuación, actualizar su devolución de llamada como esto:

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

Otros consejos

¿Por qué no almacenar "el valor anterior" como la 'UserData' de ese objeto, de la siguiente manera:


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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top