Problem with a callback function that checks user input

3 visualizaciones (últimos 30 días)
isdapi
isdapi el 30 de Nov. de 2014
Respondida: isdapi el 30 de Nov. de 2014
I have an edit box and I want that another uicontrol changes its color depending on user input. I have defined a callback function that should control that, but my problem is when the user input a negative value between the valid range because takes "-" as an invalid input.
function check(hObject,~)
inp = get(hObject,'String');
if ~isempty(strfind(inp,','))
warndlg('Invalid input. No commas are allowed','Warning');
set(tex,'BackgroundColor',[.91 .88 .88]);
elseif ~isempty(regexp(inp,'\D','once'))
warndlg('Invalid input. Only numbers are allowed','Warning');
set(tex,'BackgroundColor',[.91 .88 .88]);
elseif str2num(inp)<-360 || str2num(inp)>360
warndlg('RANGE ERROR. Valid values are between -360° and 360°','Warning');
set(tex,'BackgroundColor',[.91 .88 .88]);
elseif str2num(inp)>-360 && str2num(inp)<360
set(tex,'BackgroundColor',[.88 .91 .88])
end
end
So what can I do to solve this?

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 30 de Nov. de 2014
isdapi - why not first convert the input to a number and then decide what to do? If the string contains invalid characters, then the conversion will result in an empty matrix. For example, you could do
inp = str2num(get(hObject, 'String'));
if isempty(inp)
% input is invalid
elseif inp < -360 || inp > 360
% input is invalid
else
% input is valid
end
Try the above and see what happens!

Más respuestas (1)

isdapi
isdapi el 30 de Nov. de 2014
Thanks!

Categorías

Más información sobre App Building en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by