MATLAB: GUI Edit Text Box Value Check

block handle specifersedit box value typeif statement

During runtime I want my GUI to confirm there are values in 4 edit boxes before the user can continue filling out the 5th edit box.
The edit box values will be of numbers and not strings.
To do this I want to input code in the edit box 5 callback. Something like (pseudo-code):
if editbox1 & editbox2 & editbox3 & editbox4 isempty
listbox3 = 'All boxes in first section not populated'
else
end.
How do the edit boxes hold their values? I assume they are of the type string.
Do I need to create and array(vector), populate the array with values (string, number or Boolan value), convert said values in the array from string to numbers, check population and keep the values of type number or, can the if statement compare the 4 edit boxes in sequence to check population and use the values as type number?

Best Answer

You can insert the check in the callbacks of the first 4 edit fields. The callbacks can check the validity of the contents at first:
% In the OpeningFcn: [EDITED, was CreateFcn]
handles.EditOk = false(1, 4);
set(handles.Edit5, 'Enable', 'off');
...
guidata(ObjectH, handles); % [EDITED] Store handles in the figure

function Edit1_Callback(ObjectH, EventData)
handles = guidata(ObjectH);
Str = get(ObjectH, 'String');
Value = sscanf(Str, '%f', 1);
if ~isempty(Value) && ~isnan(Value)
StrNew = sprintf('%.8g', Value); % In the wanted format
if ~strcmp(Str, StrNew)
set(ObjectH, 'String', StrNew);
% Perhaps a warning message?
end
handles.EditOk(1) = true;
else
handles.EditOk(1) = false;
end
if all(handles.EditOk)
set(handles.Edit5, 'Enable', 'on');
else
set(handles.Edit5, 'Enable', 'off');
end
guidata(ObjectH, handles); % [EDITED] Store handles in the figure
The same can be applied in all 4 edit fields - or you can use the same callback for all fields and some further trick to identify the index of the edit box.
Now the 5th edit field is activated only if the 4 edit boxes contain valid values.