Guide: alphabet and symbols

Hi,
I have edittext and pushbutton.
If I write any letter from alphabet {a,A,b,B,c,C.....y,Y,z,Z}, then I want to enable pushbutton.
If I write any other symbols for example {" ? * & # !}, then I want to disable pushbutton.
Please, how Can I do this? . It is any different between alphabet a,b,c,d and symbols?.
Thank you

 Risposta accettata

Oleg Komarov
Oleg Komarov il 22 Mar 2012
Try this GUI
function myGUI
% Create figure
S.fh = figure('units','pixels',...
'position',[500 500 200 100],...
'menubar','none',...
'name','myGUI',...
'numbertitle','off',...
'resize','off');
% Create java edit text box
% I avoid he MATLAB uicontrol because the KeyPressFunction has a bug which
% doesn't update in real time the string
S.ed = javax.swing.JTextField();
S.ed.setHorizontalAlignment(javax.swing.JTextField.CENTER)
javacomponent(S.ed, [10 60 180 35]);
% Create push button
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Do smt');
% Add check of the string in the edit text box
set(S.ed, 'KeyReleasedCallback', @ed_krc);
function ed_krc(varargin)
% If the string contains symbols
if any(regexp(char(S.ed.getText),'\W+'))
% Disable push button
set(S.pb,'Enable','off')
else
% Otherwise enable it
set(S.pb,'Enable','on')
end
end
end

14 Commenti

john
john il 26 Mar 2012
Hi, about your code: If I write symbol "_", then is enable on. Could you change your code, to code if I write symbol "_", then is enable off.
Please, can you expand your code,... if I write symbol dot "." or any letter from alphabet, then is enable on.
Oleg Komarov
Oleg Komarov il 26 Mar 2012
Is the set '[A-z0-9.]' what you need? (where '-' means 'to')
It reads the set composed by all the letters from 'A to Z' and 'a to z' and all the numbers from '0 to 9' and the '.'
What would you use this set for? Creating names for variables? (just a guess)
john
john il 26 Mar 2012
Yes, for variables.
Do you mean '[A-z0-9.]' or '[^A-z0-9.]'. I think it works with '[^A-z0-9.]'
Walter Roberson
Walter Roberson il 26 Mar 2012
[A-z] includes some characters that are not in 'A' to Z' or 'a' to 'z'. In particular, it includes [\]^_` between the two alphabetic ranges. The upper-case letters are not immediately beside the lower-case letters in the charts.
Oleg Komarov
Oleg Komarov il 26 Mar 2012
It works with '[^A-z09.]' since it checks for characters outside the permitted set to disable the button.
Be careful, '.' is used to access structure fields.
Walter Roberson
Walter Roberson il 26 Mar 2012
The user wants the control disabled if [ or ] or \ or ^ or _ or ` are pressed, but that pattern will permit those characters.
[^A-Za-z0-9.]
john
john il 26 Mar 2012
this is my code:
if any(regexp(char(get(handles.edit,'String')),'[^a-zA-Z0-9.]'));
set(handles.edit,'String','Error');
set(handles.pushbutton4,'enable','off');
else
set(handles.pushbutton4,'enable','on');
end;
Walter Roberson
Walter Roberson il 26 Mar 2012
I think I would have used ismember:
T = char(get(handles.edit, 'String'));
if ~all(ismember(T(:), ['a':'z', 'A':'Z', '0':'9', '.', ' '])
<error>
This formulation does, though, require the introduction of the bogus space, since char() of a cell array pads end of lines with spaces.
john
john il 26 Mar 2012
Hi Oleg,
I made copy of you code to me GUI code
function varargout = menu_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
S.ed = javax.swing.JTextField();
S.ed.setHorizontalAlignment(javax.swing.JTextField.CENTER)
javacomponent(S.ed, [10 60 180 35]);
% Create push button
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Do smt');
% Add check of the string in the edit text box
set(S.ed, 'KeyReleasedCallback', @ed_krc);
function ed_krc(varargin)
% If the string contains symbols
if any(regexp(char(S.ed.getText),'\W+'))
% Disable push button
set(S.pb,'Enable','off')
else
% Otherwise enable it
set(S.pb,'Enable','on')
end
.
.
.
.
.But I got error: " ??? Undefined variable "S" or class "S.ed.getText".
Error in ==> menu>ed_krc at 107
if any(regexp(char(S.ed.getText),'\W+'))".
.
.
.where is the problem?
Walter Roberson
Walter Roberson il 26 Mar 2012
Use nested functions.
function varargout = menu_OutputFcn(hObject, eventdata, handles)
S.ed = javax.swing.JTextField(); %must initialize S before defining ed_krc
[...]
function ed_krc(varargin)
[...]
end %of ed_krc
end %of menu_OutputFcn
so ed_krc is defined entirely within the menu function; it can then share variables with it under some circumstances.
Oleg Komarov
Oleg Komarov il 26 Mar 2012
Otherwise ou have to change:
...
set(S.ed, 'KeyReleasedCallback', {@ed_krc,S});
end
function ed_krc(varargin)
S = varargin{end};
...
end
john
john il 26 Mar 2012
I don't understand. Java code is located between line 87 - 111. I added "end end", but I got error .
.
.
"??? Error: File: menu.m Line: 695 Column: 1
Unexpected MATLAB operator."
And on the line 695 is :"function edit9_Callback(hObject, eventdata, handles)"
Oleg Komarov
Oleg Komarov il 26 Mar 2012
Example with subfunctions:
function mainGUI
end
function callback
end
Example with nested functions
function mainGUI
function callback
end
end
john
john il 28 Mar 2012
Hi, can I define typ of the number? if permissible numbers are: 4,3 or 3*10^3 or 4.3*10^(-4) or 3.5e2 or 4.6e(-2).
.
.
all others are not permissible.
.
.
if any(regexp(char(get(handles.edit7,'String')),'[^0-9.*^()-]'))
set(handles.edit,'String','Error');
else
set(handles.edit, 'String','OK');
end;

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by