LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Darren's Weekly Nugget 05/24/2010

Today's nugget is a quick one.  It's been mentioned several times in the forums before, but I was surprised to discover today that I hadn't actually written a nugget about it.  So here goes...check out the Format Into String function in the code below:

 

formatintostring.png

 

This code is used to construct a table of different sections of code and the amount of time each section took to execute.  The "Code Sections" array contains the enum values that govern the execution of my state machine.  As you can see, I'm using the Format Into String function to turn each enum value into a string.  Format Into String can also turn other data types (paths and Booleans come to mind) into strings, but I have found this enum trick to be the most useful, since there seem to be many times that I need to turn an enum value into its corresponding string value for one reason or another.

Message 1 of 16
(9,664 Views)

That's a very useful technique.  Thanks for sharing it Darren.

 

You can reverse that operation with the Scan From String function.  All you need to do is wire the enum type into the element and out the other side comes the proper enum value.  If the value in the string does not match a string value or numeric value in the enum then the output will default to the value of the wired in element.

 

I find this very useful when working with state machines and the like that receive commands and/or data from a remote target via TCP/IP, serial or other.  Presently I am using the technique in an application running on an sbRIO that is being controlled by a serially interface touchscreen.  It works great.

 

Jason

Message Edited by Jason Willis on 05-24-2010 04:15 PM
Wire Warrior

Behold the power of LabVIEW as my army of Roomba minions streaks across the floor!
Message 2 of 16
(9,563 Views)
Doh, I actually could have used this this morning with an enum to make sure my run mode checking was going correctly and then concatenate it to a string to be displayed. C'mon. Nuggets by 7 a.m. please! Smiley Wink
Message 3 of 16
(9,537 Views)

Something which surely can't be left out of this nugget is the fact that the reverse also functions.

 

Following code runs flawlessly.  Of course, if the string does not exactly match one fo the enum strings, the conversion will fail.  But sometimes, this is OK.

 

enum cast.png

 

Shane

 

PS it also works if you wire an enum constant to the second function.... Just in case somebody thinks it's always returning the default value.  I use this function in some of my INI saving / Loading code.

Message Edited by Intaris on 05-25-2010 02:16 AM
Message 4 of 16
(9,456 Views)

For some reason I completely missed that Jason had already mentioned this....

 

Sorry Jason. Kudos.

 

Shane.

0 Kudos
Message 5 of 16
(9,400 Views)

Intaris wrote:

For some reason I completely missed that Jason had already mentioned this....

 

Sorry Jason. Kudos.

 

Shane.


:smileyhappy:

 

No problem Shane. At least you thought to include example code.  And as LabVIEW has taught us, a picture is worth a thousand words.

 

Jason

Wire Warrior

Behold the power of LabVIEW as my army of Roomba minions streaks across the floor!
Message 6 of 16
(9,368 Views)
Darren is not what you would call an "early bird". You're lucky there are nuggets by lunch time Smiley Very Happy
George M
National Instruments
0 Kudos
Message 7 of 16
(9,347 Views)
Format into string is one of the more useful primitives and I use it often. But I didn't know you could use it in reverse. That's cool.
PaulG.

LabVIEW versions 5.0 - 2023

“All programmers are optimists”
― Frederick P. Brooks Jr.
0 Kudos
Message 8 of 16
(9,330 Views)

PaulG. wrote:
Format into string is one of the more useful primitives and I use it often. But I didn't know you could use it in reverse. That's cool.

Just to clarify, when going in the reverse direction, the primitive being used in the snippet above is Scan From String.

0 Kudos
Message 9 of 16
(9,280 Views)

When you use "Format Into String" with "format string" unwired and an enum as an input, the format string defaults to "%s". You can explicitly define the format string to interpret enums as either the element string or the element index:

 

FormattingEnums.png

Message 10 of 16
(9,242 Views)