From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

MATLAB and LabVIEW Communication Optimal Performance

I have tried my own code,  searched through forums and examples to try and figure out best method to communicate between LabVIEW and MATLAB.  Most of the information I found was over a year old and I was wondering if there was a better current solution.  My goal is to work in LabVIEW to collect the data, process in MATLAB and return the results to LabVIEW.  I have encountered some difficulty in my search and before I delve even further in to one in particular, I was wondering if anybody had an optimal solution with this communication protocol or solutions to my errors encountered thus far.

 

I have looked at the following methods.

 

1)TCP/IP and a very good example found here: http://www.mathworks.com/matlabcentral/fileexchange/11802-matlab-tcp-ip-code-example

  • When I try to adjust even the example and communicate for my own purposes I get the following errors
    Error 63 if MATLAB server not running
  • Error 66 occurs if the TCP/IP connection is closed by the peer. In this case, Windows notices that no data is returned within a reasonable time, and it closes the TCP/IP connection. When LabVIEW attempts to communicate after Windows closes the connection, error 66 is the result. 
  • However, the example itself works perfectly and does not get these errors

 

2)Math Script Node, works but the post below states that MATLAB Node is faster.

"computing fft of a 1024x1024 matrix ten times (attached code). Result is that Matlab node version takes 0.5s versus 1.6s for Mathscript node version."

 http://forums.ni.com/t5/LabVIEW/Why-are-mathscript-performances-much-below-matlab-s/m-p/2369760/high...

 

 

3) MATLAB Node, which states it uses ActiveX Technology seemingly works well, but loses time for data transfer.

 

4) Trying to use the ActiveX functions or if there is other Automation potential.


5)Other solutions that I have not found that might be better suited.

 

Thank you for any help or suggestions in advance. 

 

0 Kudos
Message 1 of 10
(4,222 Views)

cj18,

 

I know you want to do the processing outside LabVIEW, however, I was wondering what kind of processing you are trying to do. The speed of the Mathscript Node depends on the algorithm you try to use. In addition, LabVIEW has a very complete number of functions available that can process the data while you are acquiring it, so you don't need to 'store' all this data before starting operating over it.

I would suggest you to describe the algorithm and the constraints involved to it (like the script come from another department) so we can better help you.

Barp - Control, Simulation, RTT and HIL - National Instruments
0 Kudos
Message 2 of 10
(4,168 Views)

I would have to basically agree with barp that unless there is either (a) a really good reason or (b) an absolute requirement, you're better doing the processing in LV. It will be far more efficient. I once worked with a group of aeronautical types who all used Matlab, but dropped it like a hot potato when they saw there was a better way.

 

Buuut, having said all that. If I had to do it, I would use the TCP/IP method. I would also question your analysis of the Error 66 issue. Windows doesn't close connections because it takes too long. When you attempt a TCP/IP read you specify how long to wait for the response. If you don't get a reply the read will terminate with a Error 56.

 

Error 66 means that the peer (the address/port you were talking to -- Matlab) closed the connection.

 

By the way, the 63 means that the server couldn't be found -- sort of like if the program you were trying to talk to isn't running...

 

Mike...


Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 3 of 10
(4,163 Views)

Barp and Mikeporter,

 

Thank you for your assistance:

 

The reason I need to do the processing in matlab is as you mentioned the processing script is coming from another person who has already developed it in matlab.  I almost have to treat it as a black box.

 

The TCP/IP method was interesting is that none of the errors show up when I run the example but if I try to modify it in a new VI I get the errors.

 

I have attached a simple program that just has a basic butterworth low pass filter I am trying to confirm if it works in the Matlab node.  I have done other simple codes which work, and this one does not seem the implement  the appropriate filter.  The LabVIEW signal and LabVIEW filter seem to work at the default values (but not if I change sampling rate) for the Simulation of signal, Matlab signal and Matlab filter work, but the Labview signal processed in Matlab is not working...

 

Ideally it would be bandpass filtered (0.1-30) at sampling rate of 256 Hz and further processed from there, but I can't even seem to get low pass to work in the matlab to labview communication.

 

Any help would be greatly appreciated.  Once I have that working I will have more of an idea of the constraints of the actual processing Matlab Code I will be using.

 

Thank you again.

 

-cj18

0 Kudos
Message 4 of 10
(4,126 Views)

cj18,

 

you are using the Dynamic Datatype [DDT] (blue wire) to the input of the Node. Notice that this is a 'scalar' with 'Real' type. You want to choose "1-D array of Real" for this case. This will insert a 'conversion from DDT to 1D Array" function for you. Try this and see if this would work inside the node now.

 

wrong datatype.PNG

 

PS: Even although some benchmark would indicate that Mathscript is slower, in the last release (2012-2013), there was some work within Mathscript Node to improve its performance. I would suggest you benchmark your specific algorithm with Mathscript against the solution using the node above. Maybe the 'overall time' would still be better than trying to transfer the information between both software and you can keep the original script unalterered. The reason I am insisting on this solution is because I've seen major performance improvements when you process everything in LabVIEW... 

Barp - Control, Simulation, RTT and HIL - National Instruments
0 Kudos
Message 5 of 10
(4,119 Views)
Your code shows one of the problems with the dynamic data type -- and Matlab too for that matter. You can't easily tell what the data type of a given value is.

The solution in LV is easy: Don't use express VIs.

Unfortunately, this problem is built into how Matlab works and can't be fixed.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 6 of 10
(4,100 Views)

Mike,

 

I think he can use the express VI, as long as he converts the Dynamic Datatype (DDT) into 1D-array before he does any operation to it. The express VIs have its own merits, but it is unfortunate that only outputs DDT. Converting to 1D Array makes it work better with the node.

Barp - Control, Simulation, RTT and HIL - National Instruments
0 Kudos
Message 7 of 10
(4,074 Views)

Thank you both,

I have changed the dynamic data and it seems to be working.  I tried the first time and it had error but as you said it then automatically converted it.

 

I will test the mathscript node solution as well and check timing.  Now that I have some communication protocol working I will be able to progress the project.  I will also try to figure out TCP/IP a bit more.

 

I will need to look more in to the data representation and changing of parameters within LabVIEW and Matlab itself, but will let you know how things go.

 

Thank you for your advice, help and suggestions! 

 

-cj18

0 Kudos
Message 8 of 10
(4,064 Views)

Hey guys,

I am trying to work with the connection again and running functions is fine but trying to load variables or .mat files within mathscript or matlab are both not working.  One says it runs out of memory while the other says it has potential issues finding variables within script. 

I was just wondering if it was possible to load .mat files or is the best way to save as ASCII and import to Labview from that?

 

Thanks again for any help you can give.

0 Kudos
Message 9 of 10
(3,945 Views)

Another alternative is .m files. These are text files that are easy to parse.

 

Mike...


Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 10 of 10
(3,941 Views)