필터 지우기
필터 지우기

How to accept only numbers in a edit text box?

조회 수: 24 (최근 30일)
Nu9
Nu9 2011년 8월 26일
댓글: Walter Roberson 2017년 12월 18일
hi again, i need to input values at seven edit text box but i want to show a window withe erros alert when i input leters. using guide and callback functions it's easy but now i have this at the script:
Temp = uicontrol('style','edit',...
'units','pixels',...
'position',[25 364 101 31],...
'string','Inserir Temperatura.',...
'foregroundcolor','r',...
'callback',{@Temp_call});
pause(2)
S = get(0,'userdata');
str = '0';
set(Temp,'string',str,'foregroundcolor','k')
uicontrol(Temp)
function [] = Temp_call(varargin)
% Callback for secondary GUI editbox.
S = get(0,'userdata');
set(S.ed,'string',get(gcbo,'string')) % Set gui_passdata editbox string.
and with guide when i've done another project, i use this code:
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject, handles);
can i use this code? i don't know how to use because there's no handles

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 26일
Temp = uicontrol('style','edit',...
'units','pixels',...
'position',[25 364 101 31],...
'string','Inserir Temperatura.',...
'foregroundcolor','r',...
'callback','@Temp_call');
function Temp_call(src,eventdata)
str=get(src,'String');
if isempty(str2num(str))
set(src,'string','0');
warndlg('Input must be numerical');
end
  댓글 수: 2
Jose Antonio  Salcedo Simon
Jose Antonio Salcedo Simon 2017년 12월 18일
편집: Walter Roberson 2017년 12월 18일
Hi Fangjun,
I'm a little new, could you give me a more detailed example?
I have this code:
caja_lonsat= uicontrol('style','edit','Units','normalized','pos',tex(29,:),...
'ForegroundColor','black','String',num2str(lonsat,'%4.2f'),...
'Horizontalalignment','left',...
'Backgroundcolor','white','Fontsize',12);
What else do I need? Thanks so much.
Walter Roberson
Walter Roberson 2017년 12월 18일
caja_lonsat= uicontrol('style', 'edit', 'Units', 'normalized', 'pos', tex(29,:), ...
'ForegroundColor', 'black', 'String', num2str(lonsat,'%4.2f'), ...
'Horizontalalignment', 'left', ...
'Backgroundcolor', 'white', 'Fontsize', 12, ...
'Callback', @temp_call);
Along with
function Temp_call(src,eventdata)
str = get(src, 'String');
if isnan(str2double(str))
set(src, 'String',' 0');
warndlg('Input must be numerical');
end

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

추가 답변 (1개)

Daniel Shub
Daniel Shub 2011년 8월 26일
  댓글 수: 1
Nu9
Nu9 2011년 8월 26일
but i'm not using guide and callback functions, that tips in the link will work with the first part of my code?

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

카테고리

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