5_P & S for String & File IO
5_P & S for String & File IO
9.1 INTRODUCTION
The string control and indicator is located on the Controls»Text Controls and Controls»Text Indicators
palettes to simulate text entry boxes and labels. Right-click a string control or indicator on the front
panel to select from the display types. Table 9.1 shows an example message for each display type.
194
Strings and File I/O 195
Use the operating tool or labeling tool to type or edit text in a string control. Use the positioning
tool to resize a front panel string object. To minimize the space that a string object occupies,
right-click the object and select the Visible Items»Scrollbar option from the shortcut menu.
9.2.1 Tables
Use the table control to create a table on the front panel. Use the table control located on the
Controls»All Controls»List & Table palette or the Express Table VI located on the Controls»Text
Indicators palette to create a table on the front panel. Each cell in a table is a string, and each cell
resides in a column and a row. A table is a display for a 2D array of strings. Figure 9.1 shows a
table and all its parts. Define cells in the table by using the operating tool or the labeling tool to
select a cell and typing text in the selected cell. The table displays a 2D array of strings, so you
must convert 2D numeric arrays to 2D string arrays before you can display them in a table indicator.
The row and column headers are not automatically displayed as in a spreadsheet. You must create
1D string arrays for the row and column headers.
String functions behave similarly to array functions; in fact, strings are really just arrays of ASCII
data. Use the String functions to concatenate two or more strings, extract a subset of strings from
a string, convert data into strings, and format a string for use in a word processing or spreadsheet
application. Use the String functions located on the Functions»All Functions»String palette to edit
and manipulate strings on the block diagram. String functions include the following:
● String Length—Returns in length the number of characters (bytes) string, including space
characters. For example, the String Length function returns a length of 19 for the following
string:The quick brown fox.
● Concatenate String—Concatenates input strings and 1D arrays of strings into a single
output string. For array inputs, this function concatenates each element of the array. Add
inputs to the function by right-clicking an input and selecting Add Input from the shortcut
menu or by resizing the function.
● String Subset—Returns the substring of the input string beginning at offset and containing
length number of characters. The offset of the first character in string is 0. For example, if
you use the string Volts DC+1.22683E+1; as the input, the String Subset function returns
the following substring for an offset of 6 and a length of 2: DC.
9.3.1 Converting Numeric Values to Strings with the Build Text Express VI
Use the Build Text Express VI to convert numeric values into strings. The Build Text Express VI,
located on the Functions»Output palette, concatenates an input string. If the input is not a string,
this Express VI converts the input into a string based on the configuration of the Express VI. When
you place the Build Text Express VI on the block diagram, the Configure Build Text dialog box
appears.
The dialog box in Figure 9.2 shows the Express VI configured to accept one input, voltage,
and change it to a fractional number with a precision of 4. The input concatenates on the end of the
string Voltage is. A space has been added to the end of the Voltage is string. This configuration
produces the block diagram. A probe has been added to view the value of the output string. The
Build Text Express VI concatenates the Beginning Text input, in this case the voltage value, at the
end of the configured text.
9.3.2 Converting Strings to Numeric Values with the Scan From String Function
The Scan From String function converts a string containing valid numeric characters, such as 0–9,
+, –, e, E, and period (.), to a numeric value. This function scans the input string and converts the
string according to format string. Use this function when you know the exact format of the input
text. This function can scan input string into various data types, such as numeric or Boolean, based
on the format string. Resize the function to increase the number of outputs.
For example as shown in Figure 9.3 use a format string of %f, an initial search location of 8,
and VOLTS DC+1.28E+2 as the input string, to produce an output of 128. Change the precision of
the output by changing the precision of the indicator. In format string, % begins the format specifier
and f indicates a floating-point numeric with fractional format. Right-click the function and select
Edit Scan String from the shortcut menu to create or edit a format string. The Edit Scan String
dialog box shows a configuration for the format string %4f.
Use the String functions to edit strings in ways similar to the following:
● Search for, retrieve, and replace characters or substrings within a string.
● Change all text in a string to upper case or lower case.
● Find and retrieve matching patterns within a string.
● Retrieve a line from a string.
● Rotate and reverse text within a string.
● Concatenate two or more strings.
● Delete characters from a string.
To use data in another VI, function, or application, you often must convert the data to a string
and then format the string in a way that the VI, function, or application can read. Microsoft Excel
expects strings that include delimiters, such as tabs, commas, or blank spaces. Excel uses the
delimiter to segregate numbers or words into cells. To write a 1D array of numeric values to a
spreadsheet using the Write to Binary File function, you must format the array into a string and
separate each numeric with a delimiter, such as a tab. To write an array of numeric values to a
spreadsheet using the VI, you must format the array with the Array To Spreadsheet String function
and specify a format and a delimiter. Use the File I/O VIs and functions to save strings to text and
spreadsheet files. Use the String functions to perform tasks similar to the following:
● Concatenate two or more strings.
● Extract a subset of strings from a string.
● Convert data into strings.
● Format a string for use in a word processing or spreadsheet application.
To use data in another VI, function, or application, you often must convert the data to a string and
then format the string in a way that the VI, function, or application can read. For example, Microsoft
Excel expects strings that include delimiters, such as tab, commas, or blank spaces. Excel uses the
delimiter to segregate numbers or words into cells. In many cases, you must enter one or more
format specifiers in the format string parameter of a String function to format a string. The format
specifier is the code that indicates how to format a string. The following functions format strings:
● Array To Spreadsheet String
● Spreadsheet String To Array
● Scan From String
● Format Into String
● Format Value
● Scan Value
● Scan From File
● Format Into File
To configure a string control or indicator, right-click the object and select from the following
shortcut menu items:
● Normal Display
● Backslash (‘\’) Codes Display
● Password Display
● Hex Display
● Limit to Single Line
● Update Value while Typing
● Enable Wrapping
Strings and File I/O 203
File I/O records or reads data in a file. File I/O operations pass data to and from files. Use the file
I/O VIs and functions located on the Functions»All Functions»File I/O palette to handle all aspects
of file I/O, including the following:
● Opening and closing data files
● Reading data from and writing data to files
● Reading from and writing to spreadsheet-formatted files
● Moving and renaming files and directories
● Changing file characteristics
● Creating, modifying and reading configuration files
LabVIEW can use or create the following file formats: Binary, ASCII, LVM, and TDM.
● Binary—Binary files are the underlying file format of all other file formats.
● ASCII—An ASCII file is a specific type of binary file that is a standard used by most
programs. It consists of a series of ASCII codes. ASCII files are also called text files.
● LVM—The LabVIEW measurement data file (.lvm) is a tab-delimited text file you can
open with a spreadsheet application or a text-editing application. The .lvm file includes
information about the data, such as the date and time the data was generated. This file
format is a specific type of ASCII file created for LabVIEW.
● TDM—This file format is a specific type of binary file created for National Instruments
products. It actually consists of two separate files: an XML section contains the data
attributes and a binary file for the waveform.
The VIs on the File I/O palette you use depend on the format of the files. You can read data
from or write data to files in three formats—text, binary and datalog. The format you use depends
on the data you acquire or create and the applications that will access that data. Use the following
basic guidelines to determine which format to use:
● If you want to make your data available to other applications, such as Microsoft Excel,
use text files because they are the most common and the most portable.
● If you need to perform random access file reads or writes or if speed and compact disk
space are crucial, use binary files because they are more efficient than text files in disk
space and in speed.
● If you want to manipulate complex records of data or different data types in LabVIEW,
use datalog files because they are the best way to store data if you intend to access the data
only from LabVIEW and you need to store complex data structures.
Strings and File I/O 205
You can use the default LabVIEW Data directory to store the data files LabVIEW generates, such
as .lvm or .txt files. LabVIEW installs the LabVIEW Data directory in the default file directory for
your operating system to help you organize and locate the data files LabVIEW generates. By
default, the Write LabVIEW Measurement File Express VI stores the .lvm files it generates in this
directory, and the Read LabVIEW Measurement File Express VI reads from this directory. The
Default Data Directory constant, shown at left, and the Default Data Directory property also return
the LabVIEW Data directory by default. Select Tools»Options and select Paths from the top
pull-down menu to specify a different default data directory. The default data directory differs
from the default directory which is the directory you specify for new VIs, custom controls, VI
templates, or other LabVIEW documents you create.
you are writing to a file in a loop, use low level file I/O VIs. If you are writing to a file in a single
operation, use the high-level file I/O VIs if you prefer.
Read From Spreadsheet File—Reads a specified number of lines or rows from a numeric text
file beginning at a specified character offset and converts the data to a 2D single-precision array of
numbers. The VI opens the file before reading from it and closes it afterwards. You can use this VI
to read a spreadsheet file saved in text format.
Write to/Read from Measurement File—Express VIs that write or read data to or from a text-
based measurement file (.lvm) or a binary measurement file (.tdm) format. It includes the open,
write, close and error handling functions. This file handles formatting the string with either a tab or
comma delimiter. The Merge Signals function is used to combine data into the dynamic data type
as illustrated in Figure 9.5.
Figure 9.5 Write LabVIEW Measurement File.
A relative path describes the location of a file or directory relative to an arbitrary location in the file
system. An absolute path describes the location of a file or directory starting from the top level of
the file system. Relative paths are also referred to as symbolic paths. Use relative paths in VIs to
avoid having to rework the paths when you build an application or run the VI on a different computer.
Complete the following steps to create a relative path.
1. Place the Default Directory constant on the block diagram.
2. Place the Build Path function on the block diagram.
3. Wire the Default Directory constant to the base path input of the Build Path function.
4. Right-click the name or relative path input of the Build Path function and select
Create»Constant.
5. Use the labeling tool to double-click the name or relative path constant and enter the
filename or relative path to the file. Use the syntax appropriate for your operating system.
● Windows A path consists of the drive name, followed by a colon, followed by backslash-
SUMMARY
● Strings group sequences of ASCII characters. Use the string control and indicator to simulate
text entry boxes and labels.
Strings and File I/O 209
● To minimize the space that strings object occupies right-click the object and select Show
Scrollbar from the shortcut menu.
● Use the String functions located on the Functions»All Functions»String palette to edit
and manipulate strings on the block diagram.
● Use the Build Text Express VI to convert a numeric value to a string.
● Use the Scan From String function to convert a string to a numeric value.
● Right-click the Scan From String function and select Edit Scan String from the shortcut
menu to create or edit a format string.
● Use the File I/O VIs and functions located on the Functions»File I/O palette to handle all
aspects of file I/O.
● When writing to a file, you open, create, or replace a file, write the data and close the file.
Similarly, when you read from a file, you open an existing file, read the data and close the file.
● To access a file through a dialog box, do not wire file path in the Open/Create/Replace
File VI.
● To write data to a spreadsheet file, you must format the string as a spreadsheet string,
which is a string that includes delimiters, such as tabs. Use the Format Into File function
to format string, numeric, path and Boolean data as text and write the text to a file.
Problem 9.1 Create a VI which consists of two string inputs. Find the length of each string input.
Join the strings using Concatenate String Function. Find the length of the concatenated string.
Solution The front panel and the block diagram to solve the string problem are shown in Figures
P9.1(a) and P9.1(b).
Figure P9.1
Problem 9.2 Build a VI which gets a string input. Replace a particular word in the input string
by a new word. Use the Replace Substring function for this.
Solution The front panel and the block diagram to use the string function and solve the problem
are shown in Figures P9.2(a) and P9.2(b).
Figure P9.2
Problem 9.3 Create a VI to format the date and time in the required format using Format Date/
Time String Function. Get the date and time input from Time Stamp Control.
Solution Build the front panel and the block diagram to solve the problem as shown in Figures
P9.3(a) and P9.3(b).
Figure P9.3
Problem 9.4 Use Format into String Function to get a data in the prescribed format and add
with the already available string.
Solution The front panel and the block diagram to solve the problem using the string function
are shown in Figures P9.4(a) and P9.4(b).
Figure P9.4
Strings and File I/O 211
Problem 9.5 Use the Array to Spreadsheet String Function to format an array of data in the
spreadsheet format. Use the Spreadsheet String to Array Function to convert a spreadsheet string
to an array format.
Solution The front panel and the block diagram to use the Array to Spreadsheet String Function
are shown in Figures P9.5(a) and P9.5(b).
Figure P9.5
Problem 9.6 Use Build Text Express VI to build a text by getting inputs from numeric controls.
The Configure Build Text Window is shown below. Also use Prompt User for Input Express VI to
get inputs to build a text.
Solution The front panel and the block diagram using the Build Text Express VI are shown in
Figures P9.6(a) and P9.6(b). The front panel and the block diagram using the Prompt User for
Input Express VI to get inputs to build a text are shown in Figures P9.6(c) and P9.6(d).
Problem 9.7 Create a table which consists of user names and passwords. Input a username and
a password. Check whether both the user name and password match the contents of the table. If
they are matched glow ‘Access Given’ LED, otherwise glow ‘Access Denied’ LED. Also display
the username.
Solution The front panel and the block diagram to solve the problem are shown in Figures P9.7(a)
and P9.7(b).
Strings and File I/O 213
Figure P9.7
Problem 9.8 Use the Format Into String Function to combine a text with a number.
Solution The front panel and the block diagram using the Format Into String Function are shown
in Figures P9.8(a) and P9.8(b).
Problem 9.9 Build a VI which finds the number of occurrences of a particular string in an array
of strings.
Solution The front panel and the block diagram to solve the problem are shown in Figures P9.9(a)
and P9.9(b).
Figure P9.9
Strings and File I/O 215
Problem 9.10 Build a VI to split numbers and words available in a string. Display the splitted
numbers and words in separate arrays.
Solution Create the front panel and the block diagram to solve the problem as shown in Figures
P9.10(a) and P9.10(b).
Figure P9.10
Problem 9.11 For a string input, change the properties like visible, displayed, etc. using property
nodes.
Solution The front panel and the block diagram to solve the problem are shown in Figures P9.11(a)
and P9.11(b).
Figure P9.11
Problem 9.12 Create a VI which prompts for user inputs to get two input strings. Compare each
character of string 1 with each character of string 2. Display the number of mismatches.
Solution Build the front panel and the block diagram to solve the problem as shown in Figures
P9.12(a) and P9.12(b).
Figure P9.12
Problem 9.13 Create a VI to open a file and display the size of the file (in bytes).
Solution The front panel and the block diagram to open and display the size of a file are shown
in Figures P9.13(a) and P9.13(b).
Figure P9.13
Problem 9.14 Simulate a sine wave using Simulate Signal Express VI. Store the values of all the
points of the sine wave in a file using Write LabVIEW Measurement File Express VI.
Solution The front panel and the block diagram to use Simulate Signal Express VI are shown in
Figures P9.14(a) and P9.14(b).
Figure P9.14
Problem 9.15 Create a VI to open and read a particular file. Display the file path and contents of
the file. Display the numeric data and strings in separate files.
Solution The front panel and the block diagram to create a VI to open and read a particular file
are shown in Figures P9.15(a) and P9.15(b).
Strings and File I/O 219
Figure P9.15
Problem 9.16 Create an array of numeric values and write in a file using Write LabVIEW
Measurement File. Read the same file using Read LabVIEW Measurement File. Display the read
contents in an array indicator. Also display the file created and read.
Solution The front panel and the block diagram to solve the problem are shown in Figures P9.16(a)
and P9.16(b).
Figure P9.16
Problem 9.17 Create numbers from 1 to 6 with tab as a delimiter. Write this data in a spreadsheet
file. Replace the tab delimiter by comma ( , ) and write in another spreadsheet file.
Solution Build the front panel and the block diagram as shown in Figures P9.17(a) and P9.17(b)
to create numbers from 1 to 6 with tab as a delimiter.
Figure P9.17
REVIEW QUESTIONS
EXERCISES