Error Handling Question: How to Loop Until User Enters NUMBERS(!) in GUIDE gui edit boxes and then presses an "Update" button

조회 수: 1 (최근 30일)
Hello!
I don't think I have seen the answer to this particular situation. I have a GUIDE gui with several edit boxes (Can't use AppDesigner because of other issues with AppDesigner). The user is to enter numbers into the blank edit boxes and the press an "Update" button that checks that numbers are entered, and returns (via handles) the values in each of the boxes.
THIS code doesn't work:
% --- Executes on button press in update_design_button.
function update_design_button_Callback(hObject, ~, handles)
% hObject handle to update_design_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Test for valid entry
try
Num_Decks = handles.deck_design.String;
num_decks = str2num(Num_Decks);
Num_Holes = handles.hole_design.String;
num_holes = str2num(Num_Holes);
Num_Rows = handles.row_design.String;
num_rows = str2num(Num_Rows);
catch
errmsg = warndlg('Please Enter numbers','Number Please');
uiwait(warndlg(errmsg)); %orMessage));
end
handles.num_decks = num_decks;
handles.num_holes = num_holes;
handles.num_rows = num_rows;
guidata(hObject,handles);
There is a "Close" button elsewhere that should be an available exit, but users should basically get a warning (error?) until they put numbers in all the edit boxes and then hit "Update".
Thanks!
Doug Anderson

채택된 답변

Tommy
Tommy 2020년 6월 9일
Possibly, you ought to simply exit your callback function if the input is invalid? The values will then only be stored in your app if your test succeeds, and if your test fails, the user will need to hit the update button again to rerun the test on whatever new input they'd like to try.
.
.
.
try
Num_Decks = handles.deck_design.String;
num_decks = str2num(Num_Decks);
Num_Holes = handles.hole_design.String;
num_holes = str2num(Num_Holes);
Num_Rows = handles.row_design.String;
num_rows = str2num(Num_Rows);
catch
errmsg = warndlg('Please Enter numbers','Number Please');
uiwait(errmsg); %orMessage));
% Optionally, you can clear each of the three edit texts here, before exiting.
return
end
.
.
.
You need to be sure that the values have been stored before trying to access them elsewhere.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by