DIAdem and MATLAB® Data Transfer and Interprogram Communication

Updated Feb 27, 2019

Reported In

Software

  • DIAdem

Issue Details

How can I use DIAdem to invoke The MathWorks, Inc. MATLAB® software script server to run scripts written in MATLAB® language syntax?  Also, how can I transfer data back and forth between DIAdem and the MATLAB® environment?

Solution

There is a Communicating with MATLAB shipping example in DIAdem that shows to use the OLE interface to transfer data between DIAdem and MATLAB®. The example generates data in DIAdem generates data, transfers it to MATLAB®, runs a Mscript in MATLAB®, returns the script results back to DIAdem and displays them in the VIEW pane.

Additional Information

The example is not available in legacy versions of DIAdem. If you have an old DIAdem version and the example cannot be found, you will need to perform the steps below to implement communication between DIAdem and MATLAB®:

Note: this method works only with 32-bit versions of DIAdem.

Note: this method is not recommended. It is documented for compatibility with old projects, but the shipping example approach should be used for newer projects.
  1. Download the attached ScriptLibrary.OCX into the DIAdem installation directory and register it through regsrv32, either by pressing “Windows R” or “Start\Run” to bring up the run menu. At the Run prompt type (Please replace the complete file path of ScriptLibrary.OCX with the file path in the prompt type bellow):

    For Windows 2000 & NT
    c:\winnt\system32\regsvr32 "C:\Program Files\National Instruments\<your DIAdem version>\ScriptLibrary.ocx"

    For Windows XP
    c:\Windows\system32\regsvr32 "C:\Program Files\National Instruments\<your DIAdem version>\ScriptLibrary.ocx"

    For Windows 7 and Windows 10
    regsvr32 "C:\Program Files\National Instruments\<your DIAdem version>\ScriptLibrary.ocx"

    This will register the ScriptLibrary.ocx on your computer.
  2. Next, Copy the attached file, FFTTest1.M into the "Current directory" folder as specified in the MATLAB software. To determine the current directory folder, start the MATLAB software, and from the file menu select, "Set Path". 
     
  3. Finally detach and run the script MatLabTest1.VBS from within DIAdem. 

    The script does the following
  1. Generates data in DIAdem.
  2. Diadem transfers the data to the MATLAB® software.
  3. DIAdem invokes the MATLAB® script server to run an Mscript (FFTTest1.M) that performs an FFT on the data transferred from DIAdem. 
  4. The MATLAB® software then returns the results to DIAdem.
  5. DIAdem then pauses for 5 seconds showing the results returned from the MATLAB® software.
  6. Then DIAdem calculates an FFT and displays both the MATLAB® software results and DIAdem FFT results in VIEW.
DIAdem is calling the MATLAB® software through it’s ActiveX interface, the ScriptLibrary.ocx is managing data type conversions between the MATLAB® software and DIAdem’s VBScript engine as well as the object instantiation.

The attached example “MatLabTest1.VBS” contains all the code that opens a connection between the MATLAB® environment and DIAdem, allowing for transfer of data and running of Mscripts

The following lines from the VBS script referred to in the previous paragraph create the interface:
Dim oMatLabM 'Create MatLAB Object variable
Set oMatLabM = CreateObject("DIAdem.ScriptLibrary.MATLAB")


The object variable “oMatLabM” now can be used in the DIAdem script to access the complete ActiveX interface of the MATLAB® environment.

To push a DIAdem data channel to the MATLAB® software use the following syntax:
Call oMATLAB.PutChannel("Sine_Wave")
Where “Sine_Wave” is an existing channel in DIAdem data.

To pull an array back from the MATLAB® software into the DIAdem data area, use the following syntax:
Call oMATLAB.GetChannel("FFTRes")
Where “FFTRes” is an array created in the MATLAB® environment

To Run an Mscript, use the following syntax:
Call oMATLAB.Execute("FFTTest1")

Note: The first time you run the script it my be necessary to have the MATLAB® software open. If you get an error message, "Can not create Object, Out of Memory", just start the MATLAB® software and run the script again. You should only have to do this the first time.

Note: This tool is unsupported.

MATLAB® is a registered trademark of The MathWorks, Inc. Other product and company names listed are trademarks and trade names of their respective companies.

Attachments

WAS THIS ARTICLE HELPFUL?

Not Helpful