LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
Like arrays, strings can be much more useful when you take advantage of the many built-in functions provided by LabVIEW. This section examines a few of the functions from the String subpalette of the Functions palette. You might also browse through the rest of this palette to see what other functions are built in. String Length returns the number of characters in a given string (see Figure 9.8). Figure 9.8. String Length
Concatenate Strings concatenates all input strings into a single output string (see Figure 9.9). Figure 9.9. Concatenate Strings
When first placed onto the block diagram, this function has two inputs. You can resize it with the Positioning tool (as shown in Figure 9.10) to increase the number of inputs. Figure 9.10. Using the Concatenate Strings function to build a sentence from its parts
In addition to simple strings, you can also wire a one-dimensional (1D) array of strings as input; the output will be a single string containing a concatenation of strings in the array (see Figure 9.11). Figure 9.11. Using the Concatenate Strings function to build a sentence from a string and an array of strings
In many instances, you must convert strings to numbers or numbers to strings. The Format Into String and Scan From String functions have these capabilities (as do various other functions, but we'll concentrate on these). We'll talk about Format Into String now and Scan From String in a little while. Figure 9.12. Format Into String
Simply put, Format Into String formats string, path, enumerated type, time stamp, Boolean, or numeric data as text. Figure 9.13 shows Format Into String converting the floating-point number 1.28 to the 6-byte string "1.2800." Figure 9.13. Using the Format Into String function to format numeric floating point data
Format Into String isn't just for converting numeric data to string data (although that is how it is very commonly used). You can also convert from strings, paths, enums, Booleans, and time stamps. For example, using the format string %s will convert a Boolean to the string "FALSE" or "TRUE." However, using the format string %d will convert it to the string "0" or "1." Similarly, using the format string %s will convert an enum into its text value, and using the format string %d will convert it into its integer value.
Format Into String formats the input argument (which is in numeric format) as a string, according to the format specifications in format string. These specifications are listed in detail in the LabVIEW manuals and Online Reference (search on "String Function Overview"). The function appends the newly converted string to the input wired to initial string, if there is one, and outputs the results in resulting string. The following table gives some examples of Format Into String's behavior. Table 9.2 shows the resulting string outputs for various combinations of inputs to Format Into String.
The "%" character begins the formatting specification. Given "%number1.number2," number 1 specifies the field width of the resulting string and number 2 specifies the precision (i.e., number of digits after the decimal point). An "f" formats the input number as a floating-point number with fractional format, "d" formats it as a decimal integer, and "e" formats it as a floating-point number with scientific notation. Format Into String can be resized to convert multiple values to a single string simultaneously. You can use localization codes in your format string (which are not displayed, but affect the output of all subsequent entries in the format specifier string) to specify the decimal separator for numeric output. Use "%,;" for comma decimal separator, "%.;" for period decimal separator, and "%;" for system default decimal separator. Get Date/Time String (found in the Programming>>Timing palette) outputs date string, which contains the date specified by the time stamp input, and time string, which contains the time specified by the time stamp input (see Figure 9.14). This function is useful for time stamping your data. Note that if you don't wire the time stamp input to Get Date/Time String; it will use the current time. Figure 9.14. Get Date/Time String
Format Date/Time String (found in the Programming>>Timing palette) formats a time stamp value or a numeric value as time in the format you specify using time format codes in the time format string input (see Figure 9.15). This function is much more powerful than the Get Date/Time String function for creating data/time strings. Figure 9.15. Format Date/Time String
The time format codes used by Format Date/Time String are shown in Table 9.3.
For example, using the format string "%Y-%m-%d %H:%M:%S%3u" might produce a date/time string output that looks something like "2006-03-31 15:22:58.296." (Note how the %<digit>u time format code adds a decimal point in front of the fractional seconds.) |
Категории