views:

171

answers:

1

Hi, I know that this is probably a simple problem but I am new to Matlab GUI's and basically want to get the old value which used to be stored in the text box to replace the value which has just been entered. E.g.

  1. Text box contains a valid string,
  2. User enters invalid string,
  3. Callback func, validates input and realises new input is an error and reverts to the old previous value.

How should this be implemented or done? Atm I am just using the get and set property values. Below is some sample code:

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  A: 

Simply add a new field sampledistPrev to your handles structure.

In the openingFcn of the GUI, define the property with a line like this:

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)

Then you update your callback like this:

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
Jonas
is this the only way to do such a thing in matlab? Seems kinda ugly just to have a massive structure hold everything in one big structure.
Graham
Alternatively, you could use `setappdata`/`getappdata`, or you could define a `persistent` variable, i.e. a sort-of global variable that only lives inside `sampledist_Callback`. I find the `handles` approach the simplest, and I never had problems with a too massive structure.
Jonas
@Graham - yes it's ugly. No, it's not the only way to do it, but it is the way MATLAB designers want you to do things. Matlab has a lot of stuff (no way to enforce typing of variables, pass-by-value only rules, ability to change values in the calling function from a called function) that "real" programming languages are built to eliminate or discourage. But once you realize that, you just have to roll with it sometimes.
Marc
@Marc + @Jonas, ah ill just add the fields to the struct then.Btw, on a programming language note, i love matlabs easy of plotting, huge libraries with excellent and useful functions and simple representation of images etc. as matrices but I miss all the rules and structure provided by more OO languages (e.g. C++, Java) which I have been brought up on. Is there any language which encapsulates both?
Graham
@Graham: Matlab actually has implemented OOP since r2008a - which, for example, allows passing by value now as well, for example. If you want something like Matlab that is more like a programming language, Python may be a possibility - though it as well as many other environments lack what I like maybe most about Matlab: the excellent documentation and high level of backward-compatibility.
Jonas
@Graham-MATLAB can actually directly import any Java class you write. If you do this a lot though, you may ask whether you would be better off just using Java. But part of the trade-off for MATLABs ease of use is its lack of rules. It's especially easy for non-programmers to use, which isn't true of c++ certainly, or really Java either.@Jonas, @Graham: I write object oriented code in MATLAB, but it's (1) kludgey. (2) often slow. (3) still doesn't have strong typing. As you say, though, the documentation and huge library of high level functions compensates.
Marc