08-25-2015 03:25 PM - edited 08-25-2015 03:25 PM
Hello! Recently at work I was tasked with creating code that returned something based on the state of two booleans. Something like, if boolean A and boolean B return this, if boolean A and not boolean B return something else etc.
At first i figured that the best way to do it would be in a formula node with the return value being passed through a bunch of if else statements. Then i found out that booleans are not supported in formula notes, and that formula nodes are designed for writing formulas, and not for actual programming, which makes sense.
I came up with two ways of doing this but I felt like that they were both pretty chunky and they would suck if I had more than 2 booleans to base my logic on. The following is the code with the two different methods.
I feel like this is one operation that would be much better to do in an if else statement in code and I'd love to see if there is a better way of doing this in LabVIEW.
Solved! Go to Solution.
08-25-2015 03:31 PM
One typical way of doing this in LabVIEW is to combine the two booleans into an array, then use boolean array to number. This will give you a value between 0 and 3 (inclusive). Then you can use a single case statement, or you can build an array of the desired output values and use that as an index into the array, or if there's a direct mapping from the enum, you can simply cast that number to the enumeration type.
08-25-2015 03:32 PM
Hi psammut,
build an array from your booleans. "Convert boolean array to number" to get an integer, in your case with 4 possible values. Wire this integer to the selector of a single case structure and create 4 cases ("…0", "1", "2", "3…") and put your enum constant in each case.
Reading the LabVIEW help on case structures might help too. 😄
08-25-2015 03:33 PM
I will generally build an array of the booleans and then convert to a number, using that number to index an array of possible results.
08-25-2015 03:35 PM
Thank you all! You all replied with the same answer within a minute of each other :).
-Paul
08-25-2015 03:50 PM
Everyone who said Boolean Array to Number conversion and the case structure, don't you guys get confused with the case numbering? Anything beyond two elements results in non-intuitive numbering, so I've always just used Search 1D Array (for True) to make it easier to understand.
Cheers
--------, Unofficial Forum Rules and Guidelines ,--------
'--- >The shortest distance between two nodes is a straight wire> ---'
08-25-2015 03:52 PM
The poster needs the cases where multiple items are selected, not just one of the booleans. For that case I do search 1D array for the index of True.
08-25-2015 03:53 PM
@JonDieringer wrote:
The poster needs the cases where multiple items are selected, not just one of the booleans. For that case I do search 1D array for the index of True.
Ah, I see. So then binary conversion is definitely the best option.
Cheers
--------, Unofficial Forum Rules and Guidelines ,--------
'--- >The shortest distance between two nodes is a straight wire> ---'
08-25-2015 04:03 PM
08-25-2015 04:09 PM - edited 08-25-2015 04:10 PM
@GerdW wrote:
Hi James,
don't you guys get confused with the case numbering?
Once you set the case display to binary: No! 😄
I misunderstood the initial requirement, so binary wouldn't help too much in that case (when indices are getting up past a dozen). But in the case of OP here, binary radix is ideal! Kudos
Cheers
--------, Unofficial Forum Rules and Guidelines ,--------
'--- >The shortest distance between two nodes is a straight wire> ---'