Matlab and Labview: Matlab Script Nodes and Matlab Script Server
37 views (last 30 days)
Show older comments
Hi All,
Background:
I'm working on supporting some collaborators who have online data collection and analysis software implemented in LabView. Most of my work has been in MATLAB, which is in general more convenient for the work I'm doing. To provide my contribution in an online fashion, I'm trying to find a way that they can pass their data from LabView to MATLAB without being too clumsy. The updates I need should not be too frequent (e.g. one every minute or so, but each one should be computed quickly); it would certainly be possible to do the "dumb" solution and just write out to a file from LabView and read in from MATLAB, then write out the results from MATLAB and read them in in LabView. However, I can imagine some synchronization issues with that, so I'd prefer LabView to be able to call MATLAB. The LabView documentation mentions Matlab Script Nodes, which supposedly calls the "Matlab Script Server" to execute a script. LabView Tech Note on MATLAB script nodes
Ok, so critical points / Questions here:
I've never heard of a MATLAB Script Server before, and Mathworks doesn't seem to use that terminology to talk about it. There are certainly similar sounding things on the site, but I haven't found anything that looks quite right.
To execute the online computations I want to do, you need a LOT of data to be loaded into and out of the workspace if you have to start it up clean every time you get a call to this MATLAB Script Server. What I'd really like would be to have the workspace persist between calls. Is this possible? What is the normal behavior of the "Script Server" / LabView Matlab Script nodes? Has anyone here used this before?
Also: Is there any better way to do this? I have also read notes here and other places indicating that it's possible to set up a UDP / TCP/IP connection between MATLAB and LabView. That might be an easier solution instead, but again, outside my current areas of expertise.
Thank you in advance for your help!
1 Comment
bym
on 30 Jun 2011
LabView around version 8 came out with 'mathscript', a MATLAB-like product for doing computations. So, now they have both 'MATLAB' & 'mathscript' blocks. I can tell you they are not 100% compatible. I don't know if they have kept up with MATLAB compatibility since releasing 'mathscript'; I suspect not. Sorry I couldn't provide an answer.
Answers (1)
Eric Moore
on 1 Jul 2011
I've use matlab script nodes extensively. Labview uses matlab as a COM server and the workspace is persistent. That is, you can have multiple Matlab script nodes in your vi or vi call chain and they all share a workspace. The workspace is never cleared unless you call 'clear' explicitly. So you can, if you wish, use it as a way to pass data from one run of the vi to the next.
Large amounts of data can be difficult but most of those difficulties will exist no matter how you move the data from labview to matlab. One thing to note is that marshalling the data from labview to matlab or back is not the fastest operation, but should be able to easily handle your 1/minute requirement.
My biggest issue with it is that debugging is a nightmare. Labview swallows any matlab errors, so it will just silently fail. It sounds like you already have the code written though.
Most of my use has been in saving data. I've been bitten too many times to ever use raw text files as a data format if I can help it. My other big use has been to run a frequency demodulator written in matlab, however several years ago we moved that to C for speed purposes. The data marshaling penalty was killing us, and the matlab version couldn't handle more than a few million points at a time.
Personally I find the combination to be quite nice. Many things are much easier or more clearly expressed in matlab (curve fitting in particular) but using labview (even if it looks kinda crummy) for instrument control is much quicker to develop and pleasant to code.
I'd be happy to answer some more specific questions as well.
3 Comments
Abdur Rehman
on 21 Dec 2020
Hey Eric, so right now I am trying to connect MATLAB and LABVIEW together and I came accross a problem of calling functions in Matlab Script in Labview, do you have any leads on that?
Benji Solomon
on 21 Feb 2021
Hi Eric,
I've read your comment, and I too have been using MATLAB Script Nodes extensively. I've been using it a lot with LabVIEW 2011, and now with LabVIEW 2018. Some of the issues I had when using it with LabVIEW 2011 include the need to disconnect and reconnect the input/outputs of the MATLAB Script Nodes in order to overcome an unexpected error (don't ask me why - it just seemed to do the trick), another issue was (is) sometimes un unexplained-unpredictable LabVIEW CRASH! The latter seems to be happening also in LabVIEW 2018. My guess is a memory leak somewhere, but I'm not sure.
As for the workspace, I noticed that it keeps track of all the variables I use in the MATLAB Script Nodes, and indeed it is sometimes very useful, but I was wondering:
a) What do you do when you need to run a user function in the MATLAB Script Node? Do you need to change the current directory to point to the directory in which that .m file with your user function exists, or do you copy each user function (.m file) to the MATLAB bin directory (i.e. C:\Program Files\MATLAB\R2018b\bin\win64)?
or - do you setup somehow the location of the folder where all your MATLAB functions lie. The thing is, the software which was written in LabVIEW 2011 was given to me, and there are user functions that are run within it, but I have no idea if and where the setup of pointing LabVIEW/MATLAB to those functions is.
b) Is it best to clear the workspace after I've used the data I needed - using another MATLAB Script Node, or do I need not worry about the variables that are stored in the workspace, regardless of the fact that I might be loading the same variables again, and of the size of these variables?
Finally, if you too have been experiencing unexpected LabVIEW crashes, I would like to know, and if you know what causes them and how to avoid these crashes from happening?
Thanks,
See Also
Categories
Find more on LabVIEW in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!