help on: number "or" number = True
4 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Guillaume
on 25 Oct 2018
Edited: Guillaume
on 25 Oct 2018
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.
0 Comments
More Answers (2)
Steven Lord
on 25 Oct 2018
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".
0 Comments
Valentino Pediroda
on 26 Oct 2018
1 Comment
Stephen23
on 26 Oct 2018
Edited: Stephen23
on 26 Oct 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.
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!