From: Tyler on
I have a GUI that reads in numerical data from user. What is the best way to test if A) There actually is a number in the textbox and B) To tell the user to enter a number and go back to the GUI. A pushbutton executes the code.

Thanks,
Ty
From: Andy on
"Tyler " <cool_dude07(a)hotmail.com> wrote in message <i02vua$mb5$1(a)fred.mathworks.com>...
> I have a GUI that reads in numerical data from user. What is the best way to test if A) There actually is a number in the textbox and B) To tell the user to enter a number and go back to the GUI. A pushbutton executes the code.
>
> Thanks,
> Ty

Just use a try catch block. Presumably the user enters numerical data into an edit box? So you should get the String from the edit box, and do something like:

str = get(ed, 'String');
try
val = num2str(str);
catch
errordlg('Must enter a number');
% other cleanup, such as emptying the edit box
end
From: Jan Simon on
Dear Tyler,

> I have a GUI that reads in numerical data from user. What is the best way to test if A) There actually is a number in the textbox and B) To tell the user to enter a number and go back to the GUI. A pushbutton executes the code.

What exactly is a "textbox"?
If it is a UICONTROL('Style', 'edit'), you can use the callback:
H = uicontrol('Style', 'edit', ...
'Callback', @CheckInput);

function CheckInput(ObjH, EventData)
str = get(ObjH, 'String');
if ~isempty(str)
value = sscanf(str, '%g', 1);
if isempty(value)
set(ObjH, 'String', 'NOT A NUMBER!', ...
'ForeGroundColor', [1, 0, 0]);
pause(1);
set(ObjH, 'String', '', ...
'ForeGroundColor', [0, 0, 0]);
end
end
return;

Good luck, Jan
From: Matt Fig on
For example:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = check_num()
S.fh = figure('menub','none',...
'numbert','off',...
'units','pix',...
'pos',[400 400 200 100],...
'resize','off');
S.pb = uicontrol('style','push',...
'units','pix',...
'pos',[10 10 180 30],...
'string','Check Value',...
'callback',@pb_call);
S.ed = uicontrol('style','edit',...
'units','pix',...
'pos',[10 60 180 30]);

function [] = pb_call(varargin)
% Callback for pushbutton.
N = str2num(get(S.ed,'string'));

if isempty(N)
set(S.ed,'string','');
errordlg('Value must be numeric!','GUI ERROR','modal')
uicontrol(S.ed)
else
% Do whatever you need.
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Note that str2num allows the user to enter things like 3:7 in the editbox. If you only want to allow a scalar, consider str2double.
From: Tyler on
"Andy " <theorigamist(a)gmail.com> wrote in message <i030ds$oi5$1(a)fred.mathworks.com>...
> "Tyler " <cool_dude07(a)hotmail.com> wrote in message <i02vua$mb5$1(a)fred.mathworks.com>...
> > I have a GUI that reads in numerical data from user. What is the best way to test if A) There actually is a number in the textbox and B) To tell the user to enter a number and go back to the GUI. A pushbutton executes the code.
> >
> > Thanks,
> > Ty
>
> Just use a try catch block. Presumably the user enters numerical data into an edit box? So you should get the String from the edit box, and do something like:
>
> str = get(ed, 'String');
> try
> val = num2str(str);
> catch
> errordlg('Must enter a number');
> % other cleanup, such as emptying the edit box
> end

I like this approach, but when it gives you the error, you just push the 'Ok' button and the code continues. I need the program to re-evaluate the user input AFTER the error message, and test it again. I appreciate the other suggestions, but they are too advanced for me yet.

Thanks
 | 
Pages: 1
Prev: Calculate Memory
Next: Saveas to specific size