help on: number "or" number = True
Mostrar comentarios más antiguos
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
Más respuestas (2)
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
el 26 de Oct. de 2018
1 comentario
"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.
Categorías
Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!