이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
Oleg Komarov
2012년 3월 22일
1 개 추천
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
john
2012년 3월 26일
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
2012년 3월 26일
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
2012년 3월 26일
Yes, for variables.
Do you mean '[A-z0-9.]' or '[^A-z0-9.]'. I think it works with '[^A-z0-9.]'
Walter Roberson
2012년 3월 26일
[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
2012년 3월 26일
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
2012년 3월 26일
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
2012년 3월 26일
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
2012년 3월 26일
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
2012년 3월 26일
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
2012년 3월 26일
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
2012년 3월 26일
Otherwise ou have to change:
...
set(S.ed, 'KeyReleasedCallback', {@ed_krc,S});
end
function ed_krc(varargin)
S = varargin{end};
...
end
john
2012년 3월 26일
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
2012년 3월 26일
Example with subfunctions:
function mainGUI
end
function callback
end
Example with nested functions
function mainGUI
function callback
end
end
john
2012년 3월 28일
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;
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
