GUI issue edit text boxes not empty when blank

8 ビュー (過去 30 日間)
Joakino
Joakino 2016 年 1 月 6 日
コメント済み: Walter Roberson 2016 年 1 月 6 日
I want to check if my edit-text fields in my GUI are blank, before checking if these values are valid for a given piece of equipment.
My code is:
function CheckValuesButton_Callback(hObject, eventdata, handles)
if handles.NominalWeightRadio == 1
NW = (handles.NominalWeightEdit);
if isempty(NW)
errordlg('Add Nominal Weight.','Error')
return
end
end
BOPType = char(handles.BOPTypeBox);
RamDesign = char(handles.RamDesignBox);
ID = (handles.InsideDiameterEdit);
if isempty(ID)
errordlg('Add Inside Diameter.','Error')
return
end
OD = (handles.OutsideDiameterEdit);
if isempty(OD)
errordlg('Add Outside Diameter.','Error')
return
end
[MaxWall, MaxDiam] = getMaxDims(BOPType,RamDesign);
handles.errorDims = checkDims(MaxWall, MaxDiam, ID, OD);
guidata(hObject,handles)
The problem is that even though I have not touched the edit-text boxes, they are not empty, i.e., my code continues to run, and crashes in checkDims when there are no numerical values for ID and OD. When I print the handles i get:
handles =
OutsideDiameterEdit: [1x1 UIControl]
InsideDiameterEdit: [1x1 UIControl]
PipeDescriptionEdit: [1x1 UIControl]
NominalWeightEdit: 15
Where I have inserted info for the NominalWeightEdit text box.
How can I get these to be empty, instead of [1x1 UIControl].
Thanks
Joakim
  1 件のコメント
Joakino
Joakino 2016 年 1 月 6 日
編集済み: Joakino 2016 年 1 月 6 日
Here is for example the code for my InsideDiameterEdit:
function InsideDiameterEdit_Callback(hObject, eventdata, handles)
% hObject handle to InsideDiameterEdit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.InsideDiameterEdit = str2double(get(hObject,'String'));
guidata(hObject, handles)
% Hints: get(hObject,'String') returns contents of InsideDiameterEdit as text
% str2double(get(hObject,'String')) returns contents of InsideDiameterEdit as a double

サインインしてコメントする。

回答 (1 件)

Ingrid
Ingrid 2016 年 1 月 6 日
編集済み: Ingrid 2016 年 1 月 6 日
you should use
ID = get(handles.InsideDiameterEdit,'String')
to check what the value is in the edit-field. It will be empty if you have not typed in anything. If you have typed in a value, you should use str2double() to convert it and also perform an errorcheck there (because the user might have typed in something else than a number by mistake)
  2 件のコメント
Joakino
Joakino 2016 年 1 月 6 日
編集済み: Joakino 2016 年 1 月 6 日
This works perfectly when I dont enter anything. However, if i try to enter a value in e.g. NominalWeight, with this code:
NW = get(handles.NominalWeightEdit,'String'); %THIS IS LINE 162
if isempty(NW)
errordlg('Add Nominal Weight.','Error')
return
end
I get the following error:
Error using handle.handle/get
Invalid or deleted object.
Error in Version4>CheckValuesButton_Callback (line 162)
NW = get(handles.NominalWeightEdit,'String');
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Version4 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Version4('CheckValuesButton_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by