To download NI software, including the products shown below, visit ni.com/downloads.
Overview
This example VI shows how to call GNU Octave .m file from LabVIEW
Description
LabVIEW can calculate some mathematic problems but not everything. Therefore, one must use an external software as MATLAB. However, MATLAB is not free, so if you want to make a measurement program and use it on many computers, you need multiple licenses or expensive server license. Or you can use GNU Octave - open source and free software with almost the same syntax as MATLAB, and similar capabilities and speed. Unfortunately, Octave is not directly supported by NI. In Windows OS, Octave can be called e.g. by "System Exec.vi", but the use can be tricky without comfortable Linux pipes. So, I created a simple vi to call GNU Octave in Windows OS, to read stdout and stderr and to return results and/or errors.
First of all, you have to install GNU Octave. This example uses GNU Octave version 3.6.2. You can download and run installer from SourceForge (installer includes GNU Octave bundled with OctaveForge packages and Graphical User Interface):
Depending on your system you could need Microsoft Visual C++ 2010 Redistributable Package (x86) libraries:
http://www.microsoft.com/en-us/download/confirmation.aspx?id=5555
After installation, Octave executable ("octave.exe") should be installed (depending on your operating system and GNU Octave version) in following directory:
C:\Program Files\Octave-3.6.2\bin
(* Editor comment*)
You should not install GNU Octave version 4.x or later to execute this example because “System Exec.vi” returns the value by unexpected process.
Octave is called quietly by means of "System Exec.vi" in "octave_exec.vi". Usually Octave creates a lot of stdout output when starting. To remove unwanted output, you can apply filter keyword, and only stdout output after the keyword will be returned. See a "multiply.m" Octave script. Octave errors are parsed in a simple way to create LabVIEW error, Octave warnings are ignored. In the case of error, call string of Octave is sent to the error description, so you can copy it and test it manually from windows console.
This script demonstrates simple multiplication. The source code is described as follows:
function multiply(value1,value2)
disp('this text is not displayed in the case filter match is set to R*E*S*U*L*T*= string (stars inserted to prevent false indication)')
result=value1.*value2;
printf('RESULT=%f',result)
endfunction
Let me know of any encountered problems, so I can update this simple help. Let me know if you like or dislike (and why) "octave_exec.vi".
I think there should be simpler way to communicate with octave by means of pipes (OpenG pipes?), possibly I will explore this possibility sometime.
Requirements
Steps to Implement or Execute Code
Additional Information or References
UPDATE! this is deprecated, see better interface: GOLPI at https://decibel.ni.com/content/docs/DOC-35221
Block diagram of "octave_exec.vi":
**This document has been updated to meet the current required format for the NI Code Exchange.**
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
Boys, I need some feedback! Anyone tested this stuff? Does it worked for anyone? Is it usefull for anyone? Anyone! (It is stupid to write first comment to my own document)
I attempt to multiply 3 by 4 and i get 0 as the answer and without errors,what could i be doing wrong?
The GNU Octave is working properly? You need to write me more details. What is return code and stderr from octave_exec.vi?
basically,i have downloaded the octave using the link you provided then the two V.Is that you have given and the multiply.m.i have put these 3 files in the same folder.i run test octave.vi and i get 0 as result stderr returns empty string,hence no error returns..Am thinking,since you have already tested it then this means that I am doing something wrong,if you can just give me some suggestions on what you think i could be doing wrong that might help me figure it out,,,i use windows Xp..i have trired setting changing result=value1.*value2 in .m file and setting result=2 and hence expecting that when i run test octave i will get a 2 as the result but it returns 0..(with no error returns)
And the GNU Octave is working? Did you tried to run octave separately from Labview and tested the multiply.m? The vi does nothing more than calling octave from commandline. I have tested it in winxp, unfortunately every windows OS is behaving slightly differently, and some errors shows up on stderr, but some errors shows only in stdout. Switch off the filter key and look at the full output of the command line. You can also take a look, what command is sent to System Exec.vi and try the command yourself in window command line.
awesome..i have run octave alone and that's when i figured out why i was getting the zero..basically value1 and value2 are passed from test octave.vi as floating points in string form,hence the 3 and 4 are passed as 3,0 and 4,0 respectively however multiply.m receives the the two variables as multiply(3,0,4,0) and since it's expecting only two variables it seems to me like it will pick the fast two numbers (in this case 3 and 0) and hence return the result as zero ...thank you very much
so the problem was in national decimal place separator? I hate this..
Anyway I am working on better solution using pipes. Pipes in windows are a pain, however it already works. With this solution the octave functions as some instrument. You initialize, send commands, reads variables etc. Unfortunately it is not yet finished.
so i have made some adjustments to test octave.vi and now everything works well on my computer .....
You can switch to decimal dot in "format string" of "Format into string.vi". Use this format string:
%.;multiply(%f,%f)
The "%.;" will set decimal separator as dot for the rest of the format string ("%,;" swicht to decimal comma). See Labview help for "Format Specifier Syntax".
aahh!!ok thank you very much
Hi kaero,I would like to ask one more question,supposing the file multiply.m didn't have the the line "printf(.....)",is there a way of fishing out the value of result from labview? may be by sending some commands from labview
There are at least 3 ways to get data from one software to another:
1, Through standard output (this is named pipe). This is method used in this simple interface.
2, Through special pipe. This is method I am using in new version of labview-octave interface, however it is quite complicated under windows (and very simple under linux if you use it). With this, you can directly write commands into octave and read out values.
3, Save data to file, load file in matlab. E.g. in octave you can save to csv. Or you can also find VI's for saving and reading data in Matlab's binary format. Octave can read or write into binary format.
I use Labview for measuring data, and octave for calculations. So usually in labview I store data to some file, and only call octave to calculate results.
actually my problem is this,..i have a .m file of this kind
function multiply(value1,value2)
result=value1.*value2;
endfunction
I want to be able to fish out the value of "result" without adding a printf(...) to the file..i wonder if there exists a command i can send from labview ,pass it to system exec so it it returns the value of result in standard output
As I said you can do it with method 2: you can create a new pipe to octave. And this is complicated under windows. You cannot use a simple routine such as system exec. You need to call windows api to create a new pipe to octave, open this pipe in labview and from labview read out a value of result. You do not have to add a printf to your function.
But why you do not want to add printf()? It is the easist way.
actually that's just because the .m file that i was working with is write protected and contains no printf...anyway,i hadnt been kin enough because actually the variable that i wanted to fish out from this file is a global variable so just have to create another .m script where i have it printed ,..problem solved thank you..
Hi kaero,i am using a v.i which makes more than one call to octave through system Exec.vi,however the actual launching of octave seems to takes some time and therefore increasing the total time it takes to execute this v.i,,,do you know of anyways of making the launching of octave when called by labview faster?
Hi. 2 things:
1, remove all unnecessary packages from your Octave installation. see help for 'pkg' command. If you load all Octave Forge packages every start, it can take very long time.
2, just now I am preparing a new page with GOLPI - Gnu Octave to Labview Pipes Interface, which can do everything you need. I will write a reply here when I am finished (today or tomorrow).
i have looked at the pkg command...when i type the command pkg list it appears to me that i have only one package installed...I use windows Xp but i don't know if this problem is only on XP or other systems too.....anyway,i look forward to your new page with GOLPI...thank you..
the speed of GNU Octave start depends on a lot of things, and usually builds for windows are not so polished therefore the starts can take longer. With GOLPI you have to start GNU Octave only once. See the link on the top of this document, test it and comment it.
unfortunately i haven't been to check out the files on the link because they were made with labview 2013 and i am still on 2012 but as soon as get the 2013 version then i will surely leave a comment...thank you
How do I save a Gnu Octave plot in PJEG format? I finally got to the point where I can save it as a .PS file. I try to follow the manual's instructions, but its confusing. The parenthesis that manual says to use doesn't seem to be the right syntax. Nevertheless, this is what I type:
x = linspace ( 0, 9, 21);
y = sin( x );
plot ( x, y )
print 1 figure1test 'jpeg'
Gnu Octave saves the file as jpeg.ps in my MyOctaveFiles folder, as a ps file. What I want is the program to be saved as figure1test.jpeg as a jpeg file. Any help? I've been trying to get Gnu Octave to save my plot and then as a jpeg file for hours now, and I'm getting very frustrated.
This is question about GNU Octave, please see GNU Octave manual at https://www.gnu.org/software/octave/doc/interpreter/Printing-and-Saving-Plots.html or post question in one of many GNU Octave forums.
to print figure to a jpg, use e.g. following syntax:
print -djpg 'figurefilename'