ni-ninews-1339

Upload: efx8

Post on 02-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 NI-NINews-1339

    1/81/8 www.ni.c

    Moving Data From LabVIEW Into Excel

    There are many different ways to move data from NI LabVIEW software into Microsoft Excel. Each method has advantages and drawbacks, so well explore some of the available options to helpyou find the right one for your application.

    Comparison Table

    Export TDMS File CSV File Report Gen. DIY ActiveX NI DIAdem

    Interactive

    Programmatic

    1

    Custom formatting

    Works without Excel being installed

    Automatically saves metadata

    Requires additional software

    2 3 4

    File based transfer

    Automation interface transfer

    1 data can be sent to Excel programmatically but requires manual action to save the file

    requires the free to download and installs with recent versions of LabVIEW2 TDM Excel Add-In for Microsoft Excel

    requires the3 NI LabVIEW Report Generation Toolkit for Microsoft Office

    requires4 DIAdem

    Export to Excel Right-Click Menu

    The easiest way to get visible data from LabVIEW into Excel is by right-clicking on the front panel indicator that is displaying the data you wish to export and selecting ExportExport Data To.Excel

    :Document Type NI News: YesNI Supported: Apr 7, 2011Publish Date

    http://zone.ni.com/devzone/cda/epd/p/id/2944http://sine.ni.com/nips/cds/view/p/lang/en/nid/209050http://www.ni.com/diadem/http://search.ni.com/nisearch/app/main/p/lang/en/pg/1/ap/tech/sn/catnav:pu,ssnav:dzn,n15:NINewshttp://search.ni.com/nisearch/app/main/p/lang/en/pg/1/ap/tech/sn/catnav:pu,ssnav:dzn,n15:NINewshttp://www.ni.com/diadem/http://sine.ni.com/nips/cds/view/p/lang/en/nid/209050http://zone.ni.com/devzone/cda/epd/p/id/2944
  • 7/27/2019 NI-NINews-1339

    2/82/8 www.ni.c

    You can use the method with any graph, chart, table, array, or digital data control. Graphs and charts export only the visible data, meaning you can use the graph tools toExport Data To Excelzoom in on a region of interest and export only the data you care about. Tables export only the currently selected data, whereas arrays export all of the data in the array.

    You can also use an Invoke Node to programmatically call the and methods. The following code snippet graphs a generated sine wave and theExport Data to Excel Export Plot Data To Excelexports the plot data to Excel twice in order to show the two different methods. The difference between the two methods is that the method exports only the specifiedExport Plot Data To Excelplot, whereas the method exports data from all plots. This simple example has only one plot, so both methods yield identical results.Export Data to Excel

    If you use this technique, note that Excel must be installed on the same machine as LabVIEW and there is no programmatic access to save the Excel file. Consequently, this approach is bestsuited to applications where you are interactively investigating data and looking for a quick way to send the data to Excel for analysis or distribution.

    Note: The Export To Excel feature was introduced in LabVIEW 2010 and is not available in earlier versions.

    Save to TDMS File

    NI defined the file format in response to the need for a fast, efficient way of saving well-documented measurement data. Although TDMS is notTechnical Data Management Streaming (TDMS)natively supported by Excel, there is a free Excel add-in that you can use to import your data into Excel.

    The easiest way to create a TDMS file is to use the Express VI located in the palette.Write To Measurement File File I/O

    As with all Express VIs, you will see a configuration dialog when you drop the VI on your block diagram. This particular Express VI can save to more than just TDMS, so be sure to select Binaryin the section. You have several options for how to save the file and what to do when the selected file already exists.(TDMS) File Format

    You can save to a series of files using a time- or file-size-based schedule by clicking on the button.Settings

    http://zone.ni.com/devzone/cda/tut/p/id/3727http://zone.ni.com/devzone/cda/tut/p/id/3727
  • 7/27/2019 NI-NINews-1339

    3/83/8 www.ni.c

    This results in a very simple block diagram:

    LabVIEW also provides an API for writing to TDMS files without using the Express VI for those who prefer more manual control.

    Once you have some data stored in a TDMS file, you need to make sure you have the . You may have installed the add-in when you installed LabVIEW, if nTDM Excel Add-In for Microsoft Excelyou can always download it for free. With the add-in installed, you can simply right-click on a TDMS file and select , then Excel opens and displays your data.Open WithExcel Importer

    http://zone.ni.com/devzone/cda/epd/p/id/2944http://zone.ni.com/devzone/cda/epd/p/id/2944
  • 7/27/2019 NI-NINews-1339

    4/84/8 www.ni.c

    The first worksheet in Excel always contains the file properties and other metadata.

    Each group of channels is loaded as a separate worksheet and each channel of data is shown in its own column.

  • 7/27/2019 NI-NINews-1339

    5/85/8 www.ni.c

    Once the data is in Excel, there is no dependency or link back to the original TDMS file (although the original file can be recreated using the in either LabVIEW or DIAdem)ExcelTDM DataPluginso you are free to share your workbook with any Excel user.

    Save to CSV File

    The (CSV) file format is widely supported, so you can open the file in Excel, but also import it into other spreadsheet, database, or analysis tools. CSV files arecomma-separated valueshuman-readable text files with columns separated by commas and rows separated by newlines. This normally requires us to do a little data manipulation in LabVIEW to get the measurement datainto an appropriate format because we cannot use complex data types such as waveforms, nor can we store numbers in the more efficient binary formats used internally by LabVIEW and TDMSfiles.

    You can use the VI, found in the palette, to create CSV files to open in Excel.Write To Spreadsheet File File I/O

    For a simple 1D array of numbers, you dont actually need to do any additional processing. By default, the VI uses a tab (/t) as its delimiter so you just need to specifWrite To Spreadsheet Filecomma (,) as its delimiter instead.

    A more complex example involves writing the da ta returned by an Express VI, such as the or , to a CSV file with time in one column and the measured value in tDAQ Assistant Simulate Signalnext. The code snippet below shows how you might convert the dynamic data into an array of waveforms; then and then for each waveform in the array convert all of the measured values tostrings; then formulate a time string for each sample; and finally build a 2D array of these strings to pass to the VI.Write To Spreadsheet File

    You can open the CSV file with Excel and you will see your data correctly split out into columns and rows.

    If you use the code snippet above, Excel will understand your timestamp, but probably wont format it in a useful way its unusual to deal with seconds and fractions of seconds in the businessworld that Excel was created for. To fix this, you have to format the cells containing time data with a custom format as shown below.

    http://zone.ni.com/devzone/cda/tut/p/id/12561http://en.wikipedia.org/wiki/Comma-separated_valueshttp://en.wikipedia.org/wiki/Comma-separated_valueshttp://zone.ni.com/devzone/cda/tut/p/id/12561
  • 7/27/2019 NI-NINews-1339

    6/86/8 www.ni.c

    One of the drawbacks is that you cannot save this formatting information in the CSV file so it must be manually repeated every time you bring a new file into Excel. Although once in Excel, you casave the data as an Excel file to preserve the formatting for subsequent use.

    Report Generation Toolkit for Microsoft Office

    The gives you a lot of flexibility when it comes to sending data to Excel. One of the big advantages is that you can create a templateNI LabVIEW Report Generation Toolkit for Microsoft Officeworkbook in Excel and save it as an .xltx or .xlt file and then use LabVIEW to populate your template with data when your VI runs. This code snippet shows how you can use the MS Office Repo

    Express VI to populate such a template.

    When you create the template, you can customize the formatting just like any other Excel workbook. You then name the cells where you will put data (for tables or arrays you just need to name thtop-left cell).

    Once created, you point the Express VI to the template and tell LabVIEW how you would like the data to appear in the report (either as a table or graph) and whether you want to open, save, orprint the Excel file.

    http://sine.ni.com/nips/cds/view/p/lang/en/nid/209050http://sine.ni.com/nips/cds/view/p/lang/en/nid/209050
  • 7/27/2019 NI-NINews-1339

    7/87/8 www.ni.c

    Running the code snippet above produces the output shown below. This is a very simple example of what you can do with this approach as the name suggests, the Report Generation Toolkit isoften used to automate complete reports instead of just sending simple data to Excel.

    In addition to the Express VI, the Report Generation Toolkit gives you a full API you can use to automate Excel. The example code below creates output similar to the example above but does norequire a template workbook. Also, instead of using an image of a LabVIEW graph, it uses the Excel charting tool to draw a graph.

    Running the above example yields the following output:

  • 7/27/2019 NI-NINews-1339

    8/8

    DIY ActiveX/.NET

    The Report Generation Toolkit is built on top of the Excel ActiveX interface. You can use this API either through direct ActiveX calls or the Microsoft.Office.Interop.Excel .NET assembly toautomatically create Excel files. The code snippet below produces essentially the same result as the Report Generation Toolkit example but instead uses a .NET interop assembly to communicatwith Excel. As you can see, the code ends up a little more complex but anyone with a background in ActiveX/.NET programming should feel right at home.

    Here is the output of the above code snippet:

    DIAdem as an Alternative

    If you have more advanced data needs, whether it be larger datasets, an abundance of files, or particular reporting requirements, you should also consider DIAdem. DIAdem is a tool for quicklylocating, analyzing, and reporting measurement data and is often better suited to the many demands engineers have of their data tools.

    To learn more about gaining productivity by using DIAdem rather than Excel, .read more

    Legal

    This material is protected under the copyright laws of the U.S. and other countries and any uses not in conformity with the copyright laws are prohibited, including but not limited to reproduction,DOWNLOADING, duplication, adaptation and transmission or broadcast by any media, devices or processes.

    http://zone.ni.com/devzone/cda/tut/p/id/11430http://zone.ni.com/devzone/cda/tut/p/id/11430