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

6 comentarios

Bruno Luong
Bruno Luong el 23 de Ag. de 2019
Not for me.
>> [p, status] = str2num('linear')
p =
[]
status =
logical
0
IMO you overshadow stock function
Elena Casiraghi
Elena Casiraghi el 23 de Ag. de 2019
Bruno thank you!
But How could I?
Do you have any idea?
Bruno Luong
Bruno Luong el 23 de Ag. de 2019
type
which str2num
what do you see?
Rik
Rik el 23 de Ag. de 2019
I would reccomend -all:
which str2num -all
That way you will see methods and shadowed functions as well. On my release (R2019a, only IPT and CFT installed) this returns:
>> which str2num -all
C:\Program Files\MATLAB\R2019a\toolbox\matlab\strfun\str2num.m
C:\Program Files\MATLAB\R2019a\toolbox\matlab\strfun\@opaque\str2num.m % opaque method
C:\Program Files\MATLAB\R2019a\toolbox\matlab\bigdata\@tall\str2num.m % tall method
Elena Casiraghi
Elena Casiraghi el 23 de Ag. de 2019
Thanks!
This is a good information!
Elena Casiraghi
Elena Casiraghi el 23 de Ag. de 2019
By the way, This is my result
which str2num
C:\Program Files\MATLAB\R2018a\toolbox\matlab\strfun\str2num.m

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 23 de Ag. de 2019
Editada: Stephen23 el 23 de Ag. de 2019

1 voto

"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.

3 comentarios

Elena Casiraghi
Elena Casiraghi el 23 de Ag. de 2019
Thanks Stephen!
Steven Lord
Steven Lord el 23 de Ag. de 2019
Another solution would be to avoid trying to convert that text data into numbers entirely.
svmKernel = {{'rbf'}, ...
{'gaussian'}, ...
{'linear'}, ...
{'polynomial', 'PolynomialOrder', 2}, ...
{'polynomial', 'PolynomialOrder', 3}, ...
{'polynomial', 'PolynomialOrder', 4}};
for k=1:numel(svmKernel)
t = templateSVM('Standardize',true,'KernelFunction', svmKernel{k}{:});
end
The command svmKernel{k} extracts the contents of each cell inside svmKernel in turn. Since I have cell arrays inside each of those cells, I can turn them into a comma-separated list. This is the technique shown in the "Function Call Arguments" example in the "How to Use the Comma-Separated Lists" item on that documentation page.
Elena Casiraghi
Elena Casiraghi el 24 de Ag. de 2019
Tkanks Stephen!
I didin't know that! :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Preguntada:

el 23 de Ag. de 2019

Comentada:

el 24 de Ag. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by