Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
104 ratings:
 3.04 out of 5     Rate this Document

How do I Transfer Data Between The MathWorks, Inc. MATLAB® Software Development Environment and LabVIEW?

Hardware: PXI/CompactPCI>>Controllers

Problem:
I need more information about sharing data between LabVIEW and The MathWorks, Inc. MATLAB® software environment.

Solution:
If you are a user of the MATLAB® software and want to import or export any data (for example, matrices or vectors) between the MATLAB® environment and LabVIEW, you have several options. Prior to LabVIEW 5.1, the only way to transfer data between these two environments were to use the Save and Load functions, discussed below.

LabVIEW 8.0 introduced MathScript, a loosley-typed language that is synonomous with most text-based file environments. MathScript is licensed separately but can be utilized from within LabVIEW so that you can combine graphical programming with math-oriented textual programming. See the related links below for more information on MathScript.

LabVIEW 5.1 introduced the MATLAB® Script Node. The MATLAB® Script Node makes ActiveX calls to MATLAB® software from within LabVIEW. This requires that a valid license of MATLAB® be installed on the same machine as LabVIEW. More information on the MATLAB® Script Node can be found in the related links below.

For all versions of LabVIEW, data transfer between the two environments can be performed by saving the data in a file using the MATLAB® software and reading it directly from LabVIEW, or vice versa. In the MATLAB® environment, the Save command allows you to save data in binary (*.mat) or ASCII format.

The Load command allows you to read the data back into the program.

ASCII Format
Complete the following steps if you want to import or export data between LabVIEW and the MATLAB® environment, the process is straightforward as long as you are using ASCII format.

From the MATLAB® environment to LabVIEW
  1. To save a vector or a matrix X in ASCII format with tab delimiter, enter the following in the command window or m-script file in the MATLAB® environment:
    >>SAVE filename X   -ascii -double -tabs
    This creates a file whose name is filename, and it contains the data X in ASCII format with a tab delimiter.

  2. Import the file into LabVIEW using the Read From Spreadsheet File VI located on the Programming»File I/O palette.
From LabVIEW to the MATLAB® environment
  1. To export a matrix X from LabVIEW to the MATLAB® environment, first save the data in ASCII format in LabVIEW using the Write To Spreadsheet File VI on the Programming»File I/O palette.

  2. Enter the following in the command window of the MATLAB® environment, or in the m-script file:
    >>LOAD filename
    This reads the data into the MATLAB® environment.
Binary Format
Complete the following steps if you want to import or export data between LabVIEW and MATLAB®.

From the MATLAB® environment to LabVIEW

  1. To read a .mat file in LabVIEW would require a VI to parse the file. This may be easier if each variable is saved to a separate file.
From LabVIEW to the MATLAB® environment
  1. Because the MATLAB® software saves data as .mat files, binary LabVIEW data must be converted to this format prior to transferring the data. The example SaveAsMatFile.vi in the attached zip file, available for LabVIEW 7.1 and LabVIEW 8.0, can be used to save LabVIEW data in the MATLAB® .mat format.

    The convenience of the MAT file format is that more than one variable can be saved in the same file name. The example shows seven variables on the front panel that will be saved to the same .mat file. This example can be easily modified for more or less variables.

  2. Once this data is saved from LabVIEW, it can be read into the MATLAB® environment using the following command:
    >>LOAD filename
    The Who command can then be used to display all the seven variable names, and you can display the content of them by entering the variable names at the command prompt as usual.
MATLAB® is a registered trademark of The MathWorks, Inc. Other product and company names listed are trademarks and trade names of their respective companies.

Related Links:
KnowledgeBase DIRECT-44PMH6: How do the MATLAB® Script Node and the HiQ Script Node Work?
KnowledgeBase DIRECT-44PMLT: Debugging My Script Written in The MathWorks, Inc. MATLAB® Language Syntax (or HiQ) from LabVIEW?
KnowledgeBase DIRECT-44PMKQ: Do I Need The MathWorks, Inc. MATLAB® software or HiQ to Use the Script Node?
LabVIEW 8.2 Help: MATLAB® Script Node
Products & Services: NI LabVIEW MathScript
Developer Zone Tutorial: Developing Algorithms Using LabVIEW MathScript: Part 1 – The LabVIEW MathScript Node

Attachments:

LV to MAT.zip




Report Date: 09/29/1997
Last Updated: 11/18/2009
Document ID: 11SF83W0

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit