Prompting Error for user input

Suppose I have a input box that requires an entry greater than zero before attempting to load a file.
function test_Callback(hObject, eventdata, handles)
test = str2double(get(hObject, 'String'));
if isnan(test)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
end
handles.backup.test= test;
guidata(hObject,handles)
function loaddata_Callback(hObject, eventdata, handles)
test_Callback(hObject, eventdata, handles)
[filename, pathname, Index] = ...
uigetfile({'*.txt';},['Select the File to load'],...
'\\MyDocuments\User');
This prompts the error "'Input must be a number','Error'' and allows the user to open and search for this file. What am I doing wrong?

댓글 수: 6

per isakson
per isakson 2013년 2월 16일
You ask for too much guessing. Please describe in some detail
  • how your code behaves and
  • how you want it to behave
per isakson
per isakson 2013년 2월 16일
Current behavior in a possible use case (I have not tested):
  • user clicks [loaddata]
  • gui finds e.g. a character in [test]
  • gui presents [errordlg] (,which is modal(1)) and continues
  • gui presents [uigetfile]
  • user clicks ok in [errordlg]
  • user picks a file in [uigetfile]
  • gui quits the [loaddata] callback without saving "filename"
Is this the way your code works?
(1) doc says: Note A modal dialog box prevents the user from interacting with other windows before responding. To block MATLAB program execution as well, use the uiwait function.
per isakson
per isakson 2013년 2월 17일
편집: per isakson 2013년 2월 17일
  • I cannot see how you want to provide a possibility to the user to correct the mistake (e.g. "a" in [test]).
  • What is the problem "after isnan"
  • What is the user supposed to do after clicking OK in [errordlg]?
  • "basically how the code should work", however, we rather need detailed and exactly
Edited version of your comment:
user clicks [loaddata]
if gui finds e.g. a character in [test]
gui presents [errordlg]
elseif gui finds 0
gui presents [errordlg]
end
gui presents [uigetfile]
user picks a file in [uigetfile]
So basically how the code should work is described. But the problem is that when I add the elseif statement to test_callback after isnan.
per isakson
per isakson 2013년 2월 19일
편집: per isakson 2013년 2월 19일
I insist that it is a good thing to first specify how the code should work. And it is easier to read code that is properly formatted; use [Smart Intent] in the editor. See code in my answer.
per isakson
per isakson 2013년 2월 19일
편집: per isakson 2013년 2월 20일
Yes, but what does the code that I added to my answer do?
Change
for ii = 1 : 10
to
while true

댓글을 달려면 로그인하십시오.

 채택된 답변

per isakson
per isakson 2013년 2월 14일
편집: per isakson 2013년 2월 19일

0 개 추천

You need to put this
test = str2double(get(hObject, 'String'));
if isnan(test)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
end
in a loop and not leave the loop until you have a legal value from the user.
BTW: I think it is a good habit to close functions with "end".
.
A detail: change
set(hObject, 'String', 0);
to
set(hObject, 'String', '0' );
.
An example according to my originally answer. This code gives the user ten chances to enter a correct value.
function test_Callback(hObject, eventdata, handles)
for ii = 1 : 10
test = str2double(get(hObject, 'String'));
if isnan(test)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
elseif gt(test,1)
handles.backup.test = test;
guidata(hObject,handles);
break
elseif gt(1,test)
set(hObject, 'String', '0');
errordlg('Input must be greater than 0','Error');
end
end
end

댓글 수: 3

Sean de Wolski
Sean de Wolski 2013년 2월 15일
@Per, I agree with using "end"s as a best practice but it is true that in GUIDE it creates all of the functions without them so adding end to one means you have to add it to all which takes time and makes it harder to add/change functionality.
Of course my best practice is to just write the GUI as a class or function anyway..
per isakson
per isakson 2013년 2월 20일
How do you want the program to behave?
  • one way would be to make errordlg non-modal and hope that the user sets a legal before before responding to errordlg - might confuse the user (not tested)
  • a better way would be to let test_Callback return a logical value is_ok and
if is_ok
[filename, pathname, Index] = ...
uigetfile({'*.txt';},['Select the File to load'],...
'\\MyDocuments\User');
else
some_dialog('enter a positive integer and try again')
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

태그

질문:

T
T
2013년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by