Hi, how me forbid to entering characters, other than numbers into edit text in my code
2 次查看(过去 30 天)
显示 更早的评论
Hi, how me forbid to entering characters, other than numbers into edit text in my code
function varargout = Calc(varargin)
% CALC M-file for Calc.fig
% CALC, by itself, creates a new CALC or raises the existing
% singleton*.
%
% H = CALC returns the handle to a new CALC or the handle to
% the existing singleton*.
%
% CALC('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CALC.M with the given input arguments.
%
% CALC('Property','Value',...) creates a new CALC or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Calc_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Calc_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Calc
% Last Modified by GUIDE v2.5 21-Jul-2016 14:57:10
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Calc_OpeningFcn, ...
'gui_OutputFcn', @Calc_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Calc is made visible.
function Calc_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Calc (see VARARGIN)
% Choose default command line output for Calc
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Calc wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Calc_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
global jj shift
set(handles.text1,'String','0.');
jj=0;
shift=0;
function togglebutton1_Callback(hObject, eventdata, handles)
function figure1_KeyPressFcn(hObject, evendata, handles)
ch=get(hOgbejct, 'CurrentCharacter');
code=double(ch);
strcode = num2str(code);
str=get(handles, txtWin, 'String');
str=[str, ' ', ch, '=', strcode];
set(handles.txtWin, 'String', str);
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','1') ;
else
textString =strcat(textString,'1');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton17.
function pushbutton17_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton17 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','4') ;
else
textString =strcat(textString,'4');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton18.
function pushbutton18_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton18 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
str=strcat(str,'/');
set(handles.text1,'String',str);
% --- Executes on button press in pushbutton19.
function pushbutton19_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton19 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','3') ;
else
textString =strcat(textString,'3');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton20.
function pushbutton20_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton20 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','5') ;
else
textString =strcat(textString,'5');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton21 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.') ;
jj=1;
else
textString =strcat(textString,'.');
set(handles.text1,'String',textString)
end
% --- Executes on button press in pushbutton22.
function pushbutton22_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton22 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
str=eval(str);
set(handles.text1,'String',str);
% --- Executes on button press in pushbutton23.
function pushbutton23_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton23 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','7') ;
else
textString =strcat(textString,'7');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton24.
function pushbutton24_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton24 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','6') ;
else
textString =strcat(textString,'6');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton25.
function pushbutton25_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton25 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','8') ;
else
textString =strcat(textString,'8');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton26.
function pushbutton26_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton26 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
str=strcat(str,'*');
set(handles.text1,'String',str);
% --- Executes on button press in pushbutton27.
function pushbutton27_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton27 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
str=strcat(str,'-');
set(handles.text1,'String',str);
% --- Executes on button press in pushbutton28.
function pushbutton28_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton28 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','0.') ;
else
textString =strcat(textString,'0');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton29.
function pushbutton29_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton29 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','2') ;
else
textString =strcat(textString,'2');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton30.
function pushbutton30_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton30 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','9') ;
else
textString =strcat(textString,'9');
set(handles.text1,'String',textString)
end
jj=0;
% --- Executes on button press in pushbutton31.
function pushbutton31_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton31 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
str=strcat(str,'+');
set(handles.text1,'String',str);
% --- Executes on button press in pushbutton32.
function pushbutton32_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton32 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
n= length(str);
offset=1;
len=-1;
% Calculate starting index of substring:
if offset < 0
lb= offset + n + 1; % offset from end of string
lb= max(lb, 1);
elseif offset == 0
lb= 1;
else
lb= offset;
end
% Calculate ending index of substring:
if len >= 0 %substr(string name,offset,len)
ub = lb + len - 1;
else
ub = n + len;
end
% Extract substring:
outstr= str(lb : ub);
set(handles.text1,'String',outstr);
% --- Executes on button press in pushbutton33.
function pushbutton33_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton33 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.text1,'String','0.');
% --- Executes on button press in pushbutton34.
function pushbutton34_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton34 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
sqr=sqrt(s); %For sqrt.
t=num2str(sqr);
set(handles.text1,'String',t);
% --- Executes on button press in pushbutton35.
function pushbutton35_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton35 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
e=-(s); %For +/-.
t=num2str(e);
set(handles.text1,'String',t);
% --- Executes on button press in pushbutton36.
function pushbutton36_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton36 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
percent=s/100; %For percentage
t=num2str(percent);
set(handles.text1,'String',t);
% --- Executes on button press in pushbutton37.
function pushbutton37_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton37 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
div=(1/s); %For reciprocal
t=num2str(div);
set(handles.text1,'String',t);
% --- Executes on button press in pushbutton43.
function pushbutton43_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton43 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
div=(sin(s));
t=num2str(div);
set(handles.text1,'String',t);
% --- Executes on button press in pushbutton44.
function pushbutton44_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton44 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
div=(cos(s));
t=num2str(div);
set(handles.text1,'String',t);
% --- Executes on button press in pushbutton45.
function pushbutton45_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton45 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
div=(log(s));
t=num2str(div);
set(handles.text1,'String',t);
% --- Executes on button press in pushbutton46.
function pushbutton46_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton46 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.text1,'String');
s=str2double(str);
div=(s^2);
t=num2str(div);
set(handles.text1,'String',t);
3 个评论
回答(2 个)
Walter Roberson
2016-7-27
You need to use a KeyPressCallback on the uicontrol 'style' 'edit'. When you detect an entered invalid character, you need to set() the String property to the corrected string. However, you need to keep track of the proper content of the field somewhere other than the String property: the String property that you can get() will not change until the user presses Enter in the field or a different uicontrol is given focus (in which case the cursor position will be lost.)
3 个评论
André
2018-3-16
编辑:André
2018-3-16
Try something like this:
function yourTextEdit_Callback(hObject, eventdata)
if ~isempty(str2num(hObject.String))
% your code to process that number
else
% your code to say its forbidden, liek this:
error('Forbidden')
end
end
Edit: only saw now the date of the question. Hope you have already solved the problem.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!