Number and alphabet
Mostra commenti meno recenti
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 Commenti
Oleg Komarov
il 7 Mag 2012
You cannot create variables which start from a number. This is a naming convention for variables and functions.
john
il 7 Mag 2012
John D'Errico
il 7 Mag 2012
Use a regular expression to test for this event.
Risposte (1)
Titus Edelhofer
il 7 Mag 2012
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 Commenti
john
il 7 Mag 2012
Oleg Komarov
il 7 Mag 2012
isvarname does that, try Titus' solution.
john
il 7 Mag 2012
john
il 7 Mag 2012
Titus Edelhofer
il 7 Mag 2012
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
il 7 Mag 2012
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
il 8 Mag 2012
john
il 17 Mag 2012
Walter Roberson
il 17 Mag 2012
What difficulties are you encountering?
Categorie
Scopri di più su Data Type Conversion 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!