Click here to Skip to main content
Click here to Skip to main content

Tagged as

OctaveSharp - Running GNU Octave with C#

, 10 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
OctaveSharp wrapper for using Octave in C# applications


“GNU Octave is a high-level interpreted language, primarily intended for numerical computations. It provides capabilities for the numerical solution of linear and nonlinear problems, and for performing other numerical experiments. It also provides extensive graphics capabilities for data visualization and manipulation. Octave is normally used through its interactive command line interface, but it can also be used to write non-interactive programs. The Octave language is quite similar to Matlab so that most programs are easily portable.

Octave is distributed under the terms of the GNU General Public License. “

(Text and image above belong to

Recently I had to use some Octave scripts within a C# project I’m currently working on. After searching for a solution I had to implement my own Octave wrapper.

The basic idea is to start a process and redirect the standard input, output and error streams in order to execute random commands with the Octave interpreter. One common problem is that after successfully running a command, the interpreter blocks the output stream and there is no way to know when Octave finished its job. A solution to this problem is as follows: given the fact that the last line in the execution of the process is always available you can create a random string and make Octave echo this string every time you run another command; when parsing the output, an indication that the previous command is surly executed is when you encounter your echoed string.


When starting the octave process a new random echo string is generated:

       private string StartOctave(string PathToOctaveBinaries, bool CreateWindow) {
             this.OctaveEchoString = Guid.NewGuid().ToString(); 
             OctaveProcess = new Process(); 
             OctaveProcess.OutputDataReceived +=
                  new DataReceivedEventHandler(OctaveProcess_OutputDataReceived); 
             OctaveEntryText = ExecuteCommand(null);

When executing a random command the current thread blocks until a ManualResetEvent is triggered:

        public string ExecuteCommand(string command) {
            if (OctaveProcess.HasExited)
            if (command != null) {              
            //echo the random GUID string
            OctaveProcess.StandardInput.WriteLine("\""+ OctaveEchoString + "\""); 
            return SharedBuilder.ToString(); 

And the asynchronous output data received puts all data in a StringBuilder until it comes across the echoed string.

 void OctaveProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) {
            if (e.Data == null) { 
                SharedBuilder.Append("Octave has exited with the following error message: \r\n" + OctaveProcess.StandardError.ReadToEnd());
            if (e.Data.Trim() == "ans = " + OctaveEchoString)
                SharedBuilder.Append(e.Data + "\r\n");

Using the code

Just create a new Octave class instance using one of the two constructors and pass on the full path to your octave installation.

After doing so, you can access the Octave command interpreter using the “ExecuteCommand” method. Reading results is done in the form of scalars(GetScalar), vectors(GetVector) and matrices(GetMatrix).

A simple script that calculates the transpose of a matrix is given below:

   octave = new Octave(@"D:\Download\Soft\Octave3.6.0_gcc4.6.2\bin", false); 
   double[][] m = octave.GetMatrix("result"); 

Hope this helps and if you use any of this work make sure to add a reference: Boros Tiberiu, OctaveSharp - running GNU Octave with C#, CodeProject, 2012


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tiberiu Boros
Other Research Institute For Artificial Intelligence, Ro
Romania Romania
No Biography provided

Comments and Discussions

QuestionFor complex value? [modified] PinmemberMember 103291491-Dec-14 21:20 
QuestionCode isn't working PinmemberMember 110564963-Sep-14 11:01 
QuestionUsing built-in libraries PinmemberMember 1090395025-Jul-14 12:55 
QuestionFunctions work with cd PinmemberMember 1090395030-Jun-14 1:12 
QuestionFunctions PinmemberMember 1090395030-Jun-14 0:37 
AnswerRe: Functions PinmemberMember 1095056216-Jul-14 3:45 
GeneralRe: Functions PinmemberMember 1090395023-Jul-14 5:57 
SuggestionAbout the exit on error... PinprofessionalBrisingr Aerowing7-Aug-13 9:04 
GeneralRe: About the exit on error... PinmemberMrButtle6-Oct-13 6:17 
GeneralRe: About the exit on error... PinprofessionalBrisingr Aerowing6-Oct-13 16:59 
QuestionTutorial of implementation Pinmemberbukowski317-Jun-13 5:07 
QuestionOctaveSharp for ASP.NET PinmemberGerd Schluckebier1-Nov-12 2:26 
AnswerRe: OctaveSharp for ASP.NET PinmemberTiberiu Boros6-Nov-12 12:00 
QuestionOctaveSharp PinmemberCVision4-Sep-12 5:03 
AnswerRe: OctaveSharp PinmemberTiberiu Boros4-Sep-12 9:12 
QuestionGetVector errors [modified] PinmemberMember 76977543-Apr-12 23:13 
AnswerRe: GetVector errors PinmemberTiberiu Boros10-Apr-12 10:51 
QuestionPlot 3D PinmemberMember 874500820-Mar-12 2:32 
AnswerRe: Plot 3D PinmemberTiberiu Boros23-Mar-12 5:32 
GeneralCool PinmemberXiang Zhai8-Mar-12 0:10 
GeneralRe: Cool PinmemberTiberiu Boros8-Mar-12 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 10 Apr 2012
Article Copyright 2012 by Tiberiu Boros
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid