# str2num strange behaviour

7 views (last 30 days)
Elena Casiraghi on 23 Aug 2019
Commented: Elena Casiraghi on 24 Aug 2019
Dear, I have this code:
svmKernel ={'rbf','gaussian', 'linear','2','3','4'};
for k=1:numel(svmKernel)
[p, status] = str2num(svmKernel{k});
if ~status; t = templateSVM('Standardize',true,'KernelFunction',svmKernel{indK});
else; t = templateSVM('Standardize',true,'KernelFunction','polynomial', 'PolynomialOrder', p); end
WHY str2num returns p =
Linear Function:
Parameters: [1×1 struct]
and status = 1 when issuing [p,status] = str2num('linear')?
[p, status] = str2num('linear')
Linear Function:
Parameters: [1×1 struct]
status =
logical
1
Elena Casiraghi on 23 Aug 2019
By the way, This is my result
which str2num
C:\Program Files\MATLAB\R2018a\toolbox\matlab\strfun\str2num.m

Stephen23 on 23 Aug 2019
Edited: Stephen23 on 23 Aug 2019
"str2num strange behaviour"
It is not strange behavior at all: str2num (unfortunately) relies on eval, as the str2num help clearly states "...str2num uses the eval function, which can cause unintended side effects when the input includes a function name." You provided str2num a function name to evaluate... you can easily find out which function it is evaluating:
which linear -all
As the str2num documentation recommends, you could avoid the whole problem simply by using str2double (which could be called before the loop), then using isnan.
Elena Casiraghi on 24 Aug 2019
Tkanks Stephen!
I didin't know that! :)