help on: number "or" number = True

Hi all,
I've come up with an unexpected behaviour while reading data from a file. Those data are stored as 'number|number', e.g. '05854|03998'.
The function I'm using (for which I'll complain elsewhere) automatically tries to convert everything to number, which may be fine, if it wouldn't be for the fact that str2num of a string of the kind above always gives "true".
You can easily check this by writing in the command line 654645|698965 or whatever other couple of numbers with an "|" in the middle.
Now, I understand that for some reason matlab is reading the "|" as a logical "or", but I can't understand why the expression number "or" number should give true. I've obviously tried also number "and" number, and that also gives true, so I can't really understand what is going on.
Can someone enlight me? I'm quite curious now

 Respuesta aceptada

Guillaume
Guillaume el 25 de Oct. de 2018
Editada: Guillaume el 25 de Oct. de 2018

0 votos

str2num is a fairly dangerous function to use, in most cases prefer using str2double. str2num evaluates the expression (it calls eval) so could be used to execute arbitrary code. It's got some built-in protections to stop you executing an str2num('system(''format C:\'')') which otherwise would format your hard drive, but these can probably be bypassed, so don't use str2num on unknown (user supplied) inputs.
As for your question, the logical OR operator takes logical inputs and will automatically convert numeric inputs to logical. For matlab (and the majority of computer languages), 0 is false and anything else is true. So, 05854 | 03998 is the same as true | true which is of course true. The only way you'd get false is if both numbers are 0.
By the way, I agree with Madhan, the best approach would be to fix your reading of the file so you don't have to do any conversion after the fact. Matlab has got some very powerful text reading functions which means we can probably come up with something reliable if you explain the format of your text file.

Más respuestas (2)

Steven Lord
Steven Lord el 25 de Oct. de 2018

1 voto

From the documentation for the or function and the | operator: "An element of the output array is set to logical 1 (true) if either A or B contain a nonzero element at that same array location. Otherwise, the array element is set to 0."
x | y is false only if both x and y are 0.
The documentation for the and function and the & operator is similar, but it has "both A and B" instead of "either A or B".
Valentino Pediroda
Valentino Pediroda el 26 de Oct. de 2018

0 votos

Thanks to both of you.
Regarding the "reading function", I was quite pissed by the str2num line, being the function a built in one (tdfread), which couldn't be disabled by any input parameters.
In particular, in the code I've found the following documentation.
% Try to convert the strings to create a double variable. If that
% succeeds, there may still have been empty strings that were ignored.
% Treat those as NaNs. If the conversion failed, there must have been
% strings that could not be converted. Leave the variable as char.
whose reason is because of this:
% If a
% column of the file contains only numeric data in the second and following
% rows, TDFREAD creates a double variable.
I've already corrected it by applying a "corrective" precheck to such functionality:
~islogical(str2num(x(1)))

1 comentario

Stephen23
Stephen23 el 26 de Oct. de 2018
Editada: Stephen23 el 26 de Oct. de 2018
"I've already corrected it by applying a "corrective" precheck to such functionality:"
The best solution, as Guillaume already explained, is to avoid str2num entirely and to import the data using a more robust importing tool, or perhaps writing a proper parser for that file format.

Iniciar sesión para comentar.

Categorías

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

Preguntada:

el 25 de Oct. de 2018

Editada:

el 26 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by