Example Code

Calling GNU Octave using LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

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.

 
GNU Octave installation:

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):

http://sourceforge.net/projects/octave/files/Octave%20Windows%20binaries/Octave%203.6.2%20for%20Wind...

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.

 

Interface description:

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.

 
Sample octave script "multiply.m":

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

 

Last words:

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

  • LabVIEW 2012 (or compatible)
  • GNU Octave 3.6.x


Steps to Implement or Execute Code

  1. Unzip the attached folder to your computer
  2. Open the VI " test octave.vi"
  3. Set the file path of “octave.exe” in the “Octave exe file path” file path control
  4. Set the directory of “multiply.m” is saved in the “Script/function directory path” file directory control
  5. Run the VI

 

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":

SnapCrab_octave_execvi Block Diagram_2017-10-13_6-12-1_No-00.png

 

 

 **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.

Comments
kaero
Member
Member
on

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)

alpony01
Member
Member
on

I attempt to multiply 3 by 4 and i get 0 as the answer  and without errors,what could i be doing wrong?

kaero
Member
Member
on

The GNU Octave is working properly? You need to write me more details. What is return code and stderr from octave_exec.vi?

alpony01
Member
Member
on

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)

kaero
Member
Member
on

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.

alpony01
Member
Member
on

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

kaero
Member
Member
on

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.

alpony01
Member
Member
on

so i have made some adjustments to test octave.vi and now everything works well on my computer .....test octave.PNG

kaero
Member
Member
on

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".

alpony01
Member
Member
on

aahh!!ok thank you very much

alpony01
Member
Member
on

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

kaero
Member
Member
on

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.

alpony01
Member
Member
on

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

kaero
Member
Member
on

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.

ponsy
Member
Member
on

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..

alpony01
Member
Member
on

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?

kaero
Member
Member
on

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).

alpony01
Member
Member
on

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..

kaero
Member
Member
on

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.

alpony01
Member
Member
on

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

EdSong
Member
Member
on

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.

kaero
Member
Member
on

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'