help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing an array from java to octave


From: George Kousiouris
Subject: Re: Passing an array from java to octave
Date: Mon, 23 Nov 2009 19:42:32 +0200
User-agent: Thunderbird 2.0.0.23 (Windows/20090812)


Hi Kim,all

thanks, I have been using this, however the problem is when you want to pass values from an existing java array to an octave array. The fact that the octave array type is an OctaveMatrix type (extension of  OctaveType), complicates a bit things. So I have  written this somehow crude but effective  script for  copying one by one the  elements of the java array to the  octave workspace. I  attach the code, it creates  a  10x5 array (easily made as parameters) , initializes the values  to 5 and then passes it to  octave. Just for future reference if someone else needs it.

          OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
         int rows=10;
         int columns=5;
         OctaveMatrix arr=new OctaveMatrix(rows,columns);
       
        int octave_array_index_offset_r,octave_array_index_offset_c;
       
        //Creation and initialization of test array
        int[][] a2 = new int[10][5];
         // print array in rectangular form
         for (int r=0; r<a2.length; r++) {
             for (int c=0; c<a2[r].length; c++) {
                
                 a2[r][c]=5;
                 System.out.print(" " + a2[r][c]);
                 octave_array_index_offset_r=r+1; //octave measures array indexes from 1, java from 0
                 octave_array_index_offset_c=c+1;
                //Passing of array to octave workspace
                 octave.eval("arr("+octave_array_index_offset_r+","+octave_array_index_offset_c+")="+a2[r][c]);
             }
             System.out.println("");
         }            
         
        //Saving of variables to octave file
        octave.eval("save params.m arr");
        octave.close();









Kim Hansen wrote:
2009/11/2 George Kousiouris <address@hidden>:
  
Hi all,

I want to pass a Java array to Octave, so that I can process the data
inside. Does anyone have an idea how this can be done? I have tried a
java to octave bridge (javaoctave-0.4.0.jar) however this seems to be
only for executing Octave commands inside a Java program and not
actually passing data from Java to Octave...
    

JavaOctave can transfer many different types of data between Java and Octave.

In the following simple example "a" is a 2x2 matrix that is passed
from Java to Octave and "b" is pulled out of Octave.

The example is from:
http://kenai.com/projects/javaoctave/pages/SimpleExampleOfJavaOctaveUsage
==============
 final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
 final OctaveMatrix a = new OctaveMatrix(new double[] { 1, 2, 3, 4 }, 2, 2);
 octave.put("a", a);
 final String func = "" //
         + "function res = my_func(a)\n" //
         + " res = 2 * a;\n" //
         + "endfunction\n" //
         + "";
 octave.eval(func);
 octave.eval("b = my_func(a);");
 final OctaveMatrix b = octave.get("b");
 octave.close();
==============

  


reply via email to

[Prev in Thread] Current Thread [Next in Thread]