0% found this document useful (0 votes)
8 views

5_P & S for String & File IO

Uploaded by

tansangmta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

5_P & S for String & File IO

Uploaded by

tansangmta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

STRINGS AND FILE I/O

9.1 INTRODUCTION

A string is a sequence of displayable or non-displayable ASCII characters. Strings provide a


platform-independent format for information and data. Some of the more common applications of
strings include the following:
● Creating simple text messages.
● Passing numeric data as character strings to instruments and then converting the strings to
numeric values.
● Storing numeric data to disk. To store numeric data in an ASCII file, you must first convert
numeric data to strings before writing the data to a disk file.
● Instructing or prompting the user with dialog boxes.
On the front panel, strings appear as tables, text entry boxes, and labels. LabVIEW includes
built-in VIs and functions you can use to manipulate strings, including formatting strings, parsing
strings, and other editing.

9.2 CREATING STRING CONTROLS AND INDICATORS

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

TABLE 9.1 Example message for each display type

Display Type Description Message


Normal Displays printable characters using the There are four display types.
Display font of the control. \is a backslash.
Non-printable characters generally appear as boxes.
‘\’ Codes Displays backslash codes for all There\sare\sfour\sdisplay\stypes.
Display non-displayable characters. \n\\\sis\sa\sbackslash.
Password Displays an asterisk (*) for each character *************************
Display including spaces. *********************
Hex Display Displays the ASCII value of each character 5468 6572 6520 6172 6520 666F
in hex instead of the character itself. 7572 2064 6973 706C 6179 2074
7970 6573 2E0A 5C20 6973 2061
2062 6163 6B73 6C61 7368 2E

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.

Figure 9.1 Table and all its parts.


9.3 STRING FUNCTIONS

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.

● Match Pattern—Searches for regular expression in string beginning at offset, and if it


finds a match, splits string into three substrings. If no match is found, match substring is
empty and offset past match is –1. For example, use a regular expression of + and use the
following string as the input: VOLTS DC+1.22863E+1.
The Match Pattern function returns a before substring of VOLTS DC, a match substring
of +, an after substring of 1.22863E+1, and an offset past match of 9. A regular expression
requires a specific combination of characters for pattern matching.
Strings and File I/O 197

● Match Regular Expression—Searches for a regular expression in the input string


beginning at the offset you enter and, if it finds a match, splits the string into three substrings
and any number of submatches.
● Array To Spreadsheet String—Converts an array of any dimension to a table in string
form, containing tabs separating column elements, a platform-dependent EOL character
separating rows, and, for arrays of three or more dimensions, headers separating pages.
● Build Text—Creates an output string from a combination of text and parameterized inputs.
If the input is not a string, this Express VI converts the input into a string based on the
configuration of the Express VI.
● Carriage Return Constant—Consists of a constant string containing the ASCII CR value.
● Empty String Constant—Consists of a constant string that is empty (length zero).
● End of Line Constant—Consists of a constant string containing the platform-dependent
end-of-line value.
● Format Date/Time String—Displays a time stamp value or a numeric value as time in
the format you specify using time format codes. Time format codes include the following:
%a (abbreviated weekday name), %b (abbreviated month name), %c (locale-specific date/
time), %d (day of month), %H (hour, 24-hour clock), %I (hour, 12-hour clock), %m
(month number), %M (minute), %p (a.m./p.m. flag), %S (second), %x (locale-specific
date), %X (locale-specific time), %y (year within century), %Y (year including century),
and %<digit>u (fractional seconds with <digit> precision).
● Format Into String—Formats string path, enumerated type, time stamp, Boolean, or
numeric data as text.
● Line Feed Constant—Consists of a constant string containing the ASCII LF value.
● Replace Substring—Inserts, deletes, or replaces a substring at the offset you specify in
string.
● Scan From String—Scans the input string and converts the string according to format
string.
● Search and Replace String—Replaces one or all instances of a substring with another
substring.
● Space Constant—Use this constant to supply a one-character space string to the block
diagram.
● Spreadsheet String To Array—Converts the spreadsheet string to an array of the
dimension and representation of array type. This function works for arrays of strings and
arrays of numbers.
● String Constant—Use this constant to supply a constant text string to the block diagram.
● Tab Constant—Consists of a constant string containing the ASCII HT (horizontal tab)
value.
● To Lower Case—Converts all alphabetic characters in string to lowercase characters.
Evaluates all numbers in string as ASCII codes for characters. This function does not
affect non-alphabetic characters.
● To Upper Case—Converts all alphabetic characters in string to uppercase characters.
Evaluates all numbers in string as ASCII codes for characters. This function does not
affect non-alphabetic characters.
● Trim Whitespace—Removes all white space (spaces, tabs, carriage returns, and linefeeds)
from the beginning, end, or both ends of string.

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.

Figure 9.2 Build Text Express VI.


Strings and File I/O 199

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.

Figure 9.3 Scan From String function.

9.4 EDITING, FORMATTING AND PARSING STRINGS

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.

9.5 FORMATTING STRINGS

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

9.5.1 Array to Spreadsheet String


It converts an array of any dimension to a table in string form, containing tabs separating column
elements, a platform-dependent EOL character separating rows, and, for arrays of three or more
dimensions, headers separating pages as shown.

9.5.2 Spreadsheet String to Array


It converts the spreadsheet string to an array of the dimension and representation of array type as
shown. This function works for arrays of strings and arrays of numbers. The connector pane displays
the default data types for this polymorphic function.
Strings and File I/O 201

9.5.3 Scan From String


It scans the input string and converts the string according to format string. Use this function when
you know the exact format of the input. The input can be string path, enumerated type, time stamp,
or numeric data. Alternatively, you can use the Scan From File function to scan text from a file.
The connector pane displays the default data types for this polymorphic function.

9.5.4 Format into String


Formats string path, enumerated type, time stamp, Boolean, or numeric data as text. Use the Format
Into File function to format data as text and write the text to a file.

9.5.5 Format Value


It converts a number into a regular string according to the format specified in format string and
appends this to string as shown. The connector pane displays the default data types for this
polymorphic function.

9.5.6 Scan Value


It converts characters at the beginning of string to the data type represented by default, according
to the conversion codes in format string, and returns the converted number in value and the remainder
of string after the match in output string. The connector pane displays the default data types for
this polymorphic function.

9.5.7 Scan from File


It scans text in a file for string, numeric, path, and Boolean data, converts the text to a data type,
and returns a duplicated refnum and the converted outputs in the order scanned. You can use this
function to read all the text in the file. However, you cannot use this function to determine a
starting point for the scan. To do so, use the Read From Text File function and the Scan From
String function.

9.5.8 Format into File


It formats string, numeric, path, or Boolean data as text and writes the text to a file.

9.6 CONFIGURING STRING CONTROLS AND INDICATORS

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

9.6.1 Normal Display


Right-click a string control or indicator and select Normal Display from the shortcut menu to
display all characters as typed, with the exception of non-displayable characters. Non-displayable
characters generally appear as boxes.

9.6.2 Backslash (‘\’) Codes Display


Right-click a string control or indicator and select ‘\’ Codes Display from the shortcut menu to
instruct LabVIEW to interpret characters that immediately follow a backslash (\) as a code for
non-displayable characters. The backslash mode is useful for debugging VIs and for sending non-
displayable characters to instruments, serial ports and other devices.

9.6.3 Password Display


Right-click a string control or indicator and select Password Display from the shortcut menu to
display an asterisk (*) for each character you enter into a string control, including spaces. When
you read the string data from the block diagram, you read the actual data the user entered. If you
try to copy data from the control, LabVIEW copies only the * characters.

9.6.4 Hex Display


Right-click a string control or indicator and select Hex Display from the shortcut menu to display
the ASCII value of each character in hex instead of the character itself. The Hex Display item is
useful for debugging and communicating with instruments.

9.6.5 Limit to Single Line


Right-click a string control or indicator and select Limit to Single Line from the shortcut menu to
prevent the user from entering a carriage return in a string control or indicator. However, if you
select this option, the user can copy multiline strings and paste them in the string control or indicator.
You also can use the Limit To Single Line? property to programmatically prevent a user from
entering a carriage return.

9.6.6 Update Value while Typing


Right-click a string control or indicator and select Update Value while Typing from the shortcut
menu to update the value of a control as the user enters characters instead of waiting until the user
presses the <Enter> key or otherwise ends text entry. Use this option to check the correctness of
the input or to give user feedback. You also can use the Update while Typing? property to update
the value programmatically.

9.6.7 Enable Wrapping


To disable word wrapping in a string control or indicator, right-click the string and remove the
checkmark next to the Enable Wrapping shortcut menu item. Disabling word wrapping causes the
string to wrap at line breaks instead. If you want to disable word wrapping, the string must be in
normal display mode. After you disable word wrapping, you can display a horizontal scroll bar by
right-clicking the string control or indicator and selecting Visible Items»Horizontal Scrollbar from
the shortcut menu. You also can use the Enable Wrapping property to disable word wrapping
programmatically.

9.7 BASICS OF FILE INPUT/OUTPUT

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

9.8 CHOOSING A FILE I/O FORMAT

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

9.8.1 Use of Text Files


Use text format files for your data to make it available to other users or applications, if disk space
and file I/O speed are not crucial, if you do not need to perform random access reads or writes, and
if numeric precision is not important. Text files are the easiest format to use and to share. Almost
any computer can read from or write to a text file. A variety of text-based programs can read text-
based files. Most instrument control applications use text strings. Store data in text files when you
want to access it from another application, such as a word processing or spreadsheet application.
To store data in text format, use the String functions to convert all data to text strings. Text files can
contain information of different data types. Text files typically take up more memory than binary
and datalog files if the data is not originally in text form, such as graph or chart data, because the
ASCII representation of data usually is larger than the data itself. For example, you can store the
number –123.4567 in 4 bytes as a single-precision floating-point number. However, its ASCII
representation takes 9 bytes, one for each character.
In addition, it is difficult to randomly access numeric data in text files. Although each character
in a string takes up exactly 1 byte of space, the space required to express a number as text typically
is not fixed. To find the ninth number in a text file, LabVIEW must first read and convert the
preceding eight numbers. You might lose precision if you store numeric data in text files. Computers
store numeric data as binary data, and typically you write numeric data to a text file in decimal
notation. A loss of precision might occur when you write the data to the text file. Loss of precision
is not an issue with binary files. Use the File I/O VIs and functions to read from or write to text files
and to read from or write to spreadsheet files.

9.8.2 Use of Binary Files


Storing binary data, such as an integer, uses a fixed number of bytes on disk. For example, storing
any number from 0 to 4 billion in binary format, such as 1, 1,000, or 1,000,000, takes up 4 bytes
for each number. Use binary files to save numeric data and to access specific numbers from a file
or randomly access numbers from a file. Binary files are machine readable only, unlike text files
which are human readable. Binary files are the most compact and fastest format for storing data.
You can use multiple data types in binary files, but it is uncommon.
Binary files are more efficient because they use less disk space and because you do not need
to convert data to and from a text representation when you store and retrieve data. A binary file can
represent 256 values in 1 byte of disk space. Often, binary files contain a byte-for-byte image of
the data as it was stored in memory, except for cases like extended and complex numeric values.
When the file contains a byte-for-byte image of the data as it was stored in memory, reading the
file is faster because conversion is not necessary. Text and binary files are both known as byte
stream files, which means they store data as a sequence of characters or bytes. Use the File I/O VIs
and functions to read from and write to binary files. Consider using the binary file functions if you
want to read numeric data from or write numeric data to a file or if you want to create text files for
use on multiple operating systems.

9.8.3 Use of Datalog Files


Use datalog files to access and manipulate data only in LabVIEW and to store complex data
structures quickly and easily. A datalog file stores data as a sequence of identically structured
records, similar to a spreadsheet, where each row represents a record. Each record in a datalog file
must have the same data types associated with it. LabVIEW writes each record to the file as a
cluster containing the data to store. However, the components of a datalog record can be any data
type which you determine when you create the file. For example, you can create a datalog whose
record data type is a cluster of a string and a number. Then, each record of the datalog is a cluster
of a string and a number. However, the first record could be (“abc”,1), while the second record
could be (“xyz”,7).
Using datalog files requires little manipulation which makes writing and reading much faster.
It also simplifies data retrieval because you can read the original blocks of data back as a record
without having to read all records that precede it in the file. Random access is fast and easy with
datalog files because all you need to access the record is the record number. LabVIEW sequentially
assigns the record number to each record when it creates the datalog file. You can access datalog
files from the front panel and from the block diagram. LabVIEW writes a record to a datalog file
each time the associated VI runs. You cannot overwrite a record after LabVIEW writes it to a
datalog file. When you read a datalog file, you can read one or more records at a time.

9.9 LabVIEW DATA DIRECTORY

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.

9.10 FILE I/O VIs

A typical file I/O operation involves the following process,


1. Create or open a file. Indicate where an existing file resides or where you want to create
a new file by specifying a path or responding to a dialog box to direct LabVIEW to the
file location. After the file opens, a refnum represents the file.
2. Read from or write to the file.
3. Close the file.
File I/O VIs and some File I/O functions, such as the Read from Text File and Write to Text
File functions, can perform all three steps for common file I/O operations. The VIs and functions
designed for multiple operations might not be as efficient as the functions configured or designed
for individual operations. Many File I/O VIs and functions contain flow-through parameters,
typically a refnum or path, which return the same value as the corresponding input parameter. If
Strings and File I/O 207

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.

9.10.1 Disk Streaming with Low-Level Functions


You also can use File I/O functions for disk streaming operations which save memory resources by
reducing the number of times the function interacts with the operating system to open and close
the file. Disk streaming is a technique for keeping files open while you perform multiple write
operations, for example, within a loop. Wiring a path control or a constant to the Write to Text File
function, the Write to Binary File function, or the Write to Spreadsheet File, VI adds the overhead
of opening and closing the file each time the function or VI executes. VIs can be more efficient if
you avoid opening and closing the same files frequently. To avoid opening and closing the same
file, you need to pass a refnum to the file into the loop. When you open a file, device or network
connection, LabVIEW creates a refnum associated with that file, device or network connection.
All operations you perform on open files, devices or network connections use the refnums to
identify each object.

9.10.2 High Level File I/O


High-level File I/O VIs include the following:
Write to Spreadsheet File—Converts a 2D or 1D array of single-precision numbers to a text
string and writes the string to a new ASCII file or appends the string to an existing file. You also
can transpose the data. The VI opens or creates the file before writing to it and closes it afterwards.
You can use this VI to create a text file readable by most spreadsheet applications as illustrated in
Figure 9.4.

Figure 9.4 Write to Spreadsheet File.

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.

9.11 CREATING A RELATIVE PATH

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-

separated directory names, followed by the filename. An example is


C:\DATADIR\TEST1 for a file named TEST1 in the directory DATADIR on the C
drive. You also can drag a path from Windows Explorer and place it in the path constant.
● Mac OS A path consists of the drive name, followed by colon-separated folder names,

followed by the filename. An example is HardDrive:DataFolder:Test1 for a file named


Test1 in the folder DataFolder on the volume named HardDrive.
● Linux A path consists of slash-separated directory names followed by the filename. An

example is /usr/datadirectory/test1 for a file named test1 in the directory /usr/


datadirectory.

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.

MISCELLANEOUS SOLVED PROBLEMS

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).

Figure P9.6 (Contd.)


Figure P9.6

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).

Figure P9.8 (Contd.)


Figure P9.8

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 (Contd.)


Strings and File I/O 217

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 (Contd.)


Strings and File I/O 221

Figure P9.17

REVIEW QUESTIONS

1. What are the common applications of strings?


2. What are string controls and indicators?
3. Explain the various display types with example messages.
4. Draw and explain how a table can be used to display a 2D array of strings.
5. How can numeric values be converted to strings with the Build Text Express VI?
6. Draw and explain some of the common string functions in LabVIEW.
7. List the functions of format strings and explain the use.
8. What are the commonly used file formats?
9. Mention the options available to write the front panel data to the file.
10. What is the difference between High Level and Low Level file functions?
11. What are the functions available to write data into a spreadsheet file?
12. List the advantages of Write LabVIEW Measurement File and Read LabVIEW Measurement
File.

EXERCISES

1. Create a VI to convert the input string to uppercase and lowercase.


2. Build a VI to replace a particular word in a string with a new word using the Search and
Replace String function.
3. Using the Match Pattern function check whether a given expression is available in the
input string. Also display the portion of the string available before the match and after the
match, and the offset of the string after the match.
4. Build a VI to create a substring from an input string by providing the offset of the substring
and length of the substring. Use the String Subset function for this. Also find the reversed
and rotated (first character last) version of the substring.
5. Use the Scan From String function to find a part of the string in the prescribed format. Also
display the remaining string and offset after the scan.
6. Use the Pick Line function to separate a particular line from a multi-line text.
7. Build a VI to find the number of matches of a particular character in a string.
8. Build a VI which gets two arrays of strings as inputs. Compare each element of array1 with
all the elements of array2. In a table display 1s for matches and 0s for non-matches.
9. In the previous example display array elements along with 0s and 1s in the table.
10. In the previous example use string controls instead of string arrays.
11. Create a VI to find whether the input string is a palindrome or not.
12. Create a VI to write the contents of a string control in a file whose path is specified.
13. Generate a 2D numeric array using random numbers. Display the generated values in a
waveform graph. Write the same values in a spreadsheet file. While writing in a spreadsheet
file add column headings. Let the column headings be Waveform1, Waveform 2 and
Waveform 3.
14. Create a VI to read a file and display its contents in a string indicator.
15. Read the same file which was read in the previous example using low level file functions.
16. Read a spreadsheet file which consists of multiple columns. Display the contents in a
two-dimensional array.

You might also like