Number and alphabet

Hi,
I have edittext and pushbutton.
If I write with "sym(' ')" for example a1 or R2 or alfa34...matlab works good. If I write any text in this format a1 into edittext, then I want to enable pushbutton.
But if the firts is number for example 1a or 45R or 89gama, then matlab generate error. So if I write any text in this format 2a into edittext, then I want to disable pushbutton.
Please, how Can I do this?
Thank you

댓글 수: 3

Oleg Komarov
Oleg Komarov 2012년 5월 7일
You cannot create variables which start from a number. This is a naming convention for variables and functions.
john
john 2012년 5월 7일
Yes, I understand...but I need avoid, that somebody will insert variables which start from a number.
John D'Errico
John D'Errico 2012년 5월 7일
Use a regular expression to test for this event.

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

답변 (1개)

Titus Edelhofer
Titus Edelhofer 2012년 5월 7일

1 개 추천

Hi,
you can use isvarname for this purpose, something like
str = get(handles.edit1, 'string');
if isvarname(str))
set(handles.pushbutton1, 'enable', 'on')
else
set(handles.pushbutton1, 'enable', 'off')
end
Titus

댓글 수: 9

john
john 2012년 5월 7일
and also I need...If I write only number or a1 or R55, then enable pushbutton.
It is possible?
Oleg Komarov
Oleg Komarov 2012년 5월 7일
isvarname does that, try Titus' solution.
john
john 2012년 5월 7일
Sorry guys,
but if I write number for example 53, pushbutton is not enabled....I think it is not working..
john
john 2012년 5월 7일
where can be a problem? :(
Titus Edelhofer
Titus Edelhofer 2012년 5월 7일
Hi,
do I understand right: "only a number" should enable as well, although it's not a variable name? Then change the if to
if isvarname(str) || ~isnan(str2double(str))
Titus
Walter Roberson
Walter Roberson 2012년 5월 7일
Should only non-negative integers be allowed like 53, or should negatives be allowed? Floating point numbers? Exponential format? Titus's use of str2double() will allow numbers in all formats, including -3.83456e-009
john
john 2012년 5월 8일
Titus, yes only number or string in form "R43" or "beta9".
Walter, only positive floating point numbers without zero....maybe it can be used
if any(regexp(char(get(handles.edit1,'String')),'[^1-9.]'))
set(handles.pushbutton1,'enable','off');
else
set(handles.pushbutton1,'enable','on');
john
john 2012년 5월 17일
I think, that command "~isvarname(string) || isnan(str2double(string))" doesn't work. Do you have any Idea?
,
,
if ~isvarname(string) || isnan(str2double(string))
set(handles.pushbutton1,'enable','off');
else
set(handles.pushbutton1,'enable','on');
end;
Walter Roberson
Walter Roberson 2012년 5월 17일
What difficulties are you encountering?

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

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2012년 5월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by