Comma Separated Values, or CSV, is a format in which strings are saved in a text file. In this format, values are saved in string format in multiple rows. Each row consists of multiple values separated by commas as delimiters. These commas can be used as means to separate values from each row. Reading CSV files into LabVIEW is a common task that enables you to read data or values stored in CSV files in an easy and convenient way.
A CSV file consists of comma separated data stored in multiple lines. The following lines are an example of the contents of a CSV file;
As an example, assume that the lines are saved in a CSV file called as "myData.csv."
Launch LabVIEW 8 software, create a new VI by clicking on "New VI" in the splash window. Save it as "importCSV.vi." In the diagram window for the "importCSV.vi," drag and drop the following components from the Funtionals Palette; "Read From Text File," "Spreadsheet String to Array" and "Line Feed" constant from the strings palette.
The "Read From Text File" block accepts as an input the file path to the CSV file. Right click on the block and from the list that pops up, click on "Create Constant" for the file path input. Type the complete path to the CSV file in the constant. For example, if the "myData.csv" file is located on the "C" drive, type "C:\myData.csv" in the "File Path" constant.
The output of the "Read From Text File" block is a text string commonly known as a spreadsheet string. This spreadsheet string consists of all the contents of the CSV file.
Connect the output string from the previous step to the input of "Spreadsheet String to Array" block. The output of "Spreadsheet String to Array" block is an array of strings. Use the "Line Feed" constant as a delimiter to separate the rows into a single dimension array of strings by connecting it to the "Delimiter" input of the "Spreadsheet String to Array" block. Each row is separated and populated into the array as an individual string element of the array.
Create a For Loop in the diagram window of the LabVIEW program. Connect the array of individual rows to the For Loop. Right click at the point where the array is connected to the For Loop and select "Enable Indexing." This ensures that for each iteration of the For Loop only one element from the array is accepted as input. It will also ensure that the number of For Loop iterations will be equal to the size of the array.
Inside the For Loop use another "Spreadsheet String to Array" block. This time use a comma as a delimiter. In every For Loop iteration a different row will be parsed into an array of string elements. The values from the CSV file import into LabVIEW as a single chunk of data, separated into individual rows and then parsed into separate values.
Things you need
- Computer with Windows XP or Windows 7 Operating System
- LabVIEW Software Development Platform