How can I classify a variable if it is a string or a number?
2 次查看(过去 30 天)
显示 更早的评论
I want to display if the costumer has introduced a number or a string when is requested.
0 个评论
采纳的回答
Benjamin Avants
2014-5-13
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 个评论
Benjamin Avants
2014-5-13
True... so you may want to check for specific cases like that if they are likely to happen. Thankfully, there aren't that many.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!