03-13-2017 06:19 PM
Hi guys,
I need to create a VI with if-else statements. The problem is that there is 5 such cases. Is there any better solution than using nested cases structure?
To be specific, I want to set a certain wait time for a timer for different input frequencies, ie:
if frequency <500 then time = 3000 ms
if 500<= frequency < 1000 then time = 2500 ms
if 1000 <= frequency <5000 then time = 3000 ms
and so on, I will get probably up to ten such ranges.
I would appreciate any idea! 10 nested cases would look rather silly...
Solved! Go to Solution.
03-13-2017 06:36 PM
you can wire the numeric value directly to the case selector and set a number of ranges (don't forget to set a default case)
http://zone.ni.com/reference/en-XX/help/371361K-01/glang/case_structure/
http://zone.ni.com/reference/en-XX/help/371361K-01/lvhowto/case_selector_values/
03-13-2017 08:29 PM
The case structure can accept booleans (and error clusters), numbers (integers), strings, and enums to its case selector tunnel. In your case, a number. And you can have ranges too. So your cases will be "..499", "500..1000", etc.
03-14-2017 12:12 AM
I've done something like this if I expected the values to change:
(As shown, this makes the case statement also able to process floating point numbers, but is a little less self-documenting.)
You can also use Boolean Array to Number instead of the 1D Search Array, but I feel my method is a bit easier to understand. I will often use code that seems more straightforward but less optimized to more clearly show what is happening if I feel the performance hit is negligible. It is my thought process that creating developer-friendly code helps make it easy to maintain.
03-14-2017 08:30 AM
@billko wrote:
You can also use Boolean Array to Number instead of the 1D Search Array, but I feel my method is a bit easier to understand.
If you do use the boolean array to number, changing the radix view on the case structure to be binary helps sort out what case will be called. But I agree that it still isn't very readable.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
16 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord
03-14-2017 09:13 AM
Thanks, it works and looks good!
03-14-2017 12:00 PM
Personally, I would just threshold into an array of frequencies and use the correctly rounded output to index into an array of times. No case structure needed. 🙂
03-14-2017 12:25 PM
@altenbach wrote:
Personally, I would just threshold into an array of frequencies and use the correctly rounded output to index into an array of times. No case structure needed. 🙂
That's because you don't play fair. 😉
03-14-2017 02:41 PM - edited 03-14-2017 05:48 PM
@billko wrote:
@altenbach wrote:
Personally, I would just threshold into an array of frequencies and use the correctly rounded output to index into an array of times. No case structure needed. 🙂
That's because you don't play fair. 😉
Fair enough. 😄
Here's how it could look like. Easily expandable by just adding elements to the array constants. No code changes needed and everything visible at all times. (adding cases to a case structure is much more work, hides more and more code, and is also error prone!). Note that the array of frequencies needs to be sorted, of course.
The code might need to be tweaked depending if you want to include the boundaries into the lower or upper range, so modify as needed. Of course boundaries in DBL are not necessarily what you think they are, so be careful)
03-15-2017 08:26 PM - edited 03-15-2017 08:27 PM
@altenbach wrote:
@billko wrote:
@altenbach wrote:
Personally, I would just threshold into an array of frequencies and use the correctly rounded output to index into an array of times. No case structure needed. 🙂
That's because you don't play fair. 😉
Fair enough. 😄
Here's how it could look like. Easily expandable by just adding elements to the array constants. No code changes needed and everything visible at all times. (adding cases to a case structure is much more work, hides more and more code, and is also error prone!). Note that the array of frequencies needs to be sorted, of course.
The code might need to be tweaked depending if you want to include the boundaries into the lower or upper range, so modify as needed. Of course boundaries in DBL are not necessarily what you think they are, so be careful)
I am ALWAYS learning stuff from you. Thank you!