How can I classify a variable if it is a string or a number?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Toni
am 13 Mai 2014
Kommentiert: Benjamin Avants
am 13 Mai 2014
I want to display if the costumer has introduced a number or a string when is requested.
0 Kommentare
Akzeptierte Antwort
Benjamin Avants
am 13 Mai 2014
Use the class() function.
str = 'Test';
num = 1;
strcmp(class(str),'char')
true
strcmp(class(num),'double')
true
Of course, if the entry was prompted with inputdlg, all responses will be returned as strings. In that case, you'll need to try and convert the string to a number and examine the result. If str2num or str2double can't convert a string, they return NaN.
2 Kommentare
Benjamin Avants
am 13 Mai 2014
True... so you may want to check for specific cases like that if they are likely to happen. Thankfully, there aren't that many.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!