string that may or may not need to be converted to num...
1 view (last 30 days)
Show older comments
I am bringing a string into a function. I need to convert it to num ONLY if it's intended as a number like 2, 40, 100... but when the user enters it as something else, leave it as a string. Can that be done?
Thanks!
0 Comments
Accepted Answer
Bish Erbas
on 24 Sep 2018
Edited: Bish Erbas
on 24 Sep 2018
Yes. Take a look at the function below:
str2num
Edit: OP clarified their question and one way to accomplish what they need would be using the following function:
Edit2: Use str2double instead. See comments section for details.
5 Comments
Bish Erbas
on 24 Sep 2018
@Stephen Cobeldick is absolutely right. Thanks for the helpful reminder. Old habits die hard. I gotta learn to remember using the more efficient str2double
Walter Roberson
on 24 Sep 2018
s = 'two';
t = str2double(s);
if isnan(t); t = s; end
More Answers (1)
Walter Roberson
on 24 Sep 2018
str2double() and test for nan, keep the original if you got nan.
0 Comments
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!