2 Replies Latest reply: Mar 6, 2014 6:02 AM by jwenting RSS

    I have a .bin file created in MATLAB and, 400x500 dimensions of a floating number matrix inside this file. I want to transfere the values of this matrix into a float Array or an Arraylist in Java.

    27b5cdf2-c8fc-4007-baff-3d9334b6ade1

      The problem is, the values are completely different than they are saved in MATLAB, and probably I need to shift the values after transforming them into byte arrays.

      I am looking for any of a solution.

      My Java code which reads values of floating numbers from a MATLAB bin file as follows:

       

       

      import java.io.*;

      import java.io.File;

      import javax.imageio.ImageIO;

      import java.nio.ByteBuffer; // may be useful?

       

       

      public class floatingNumberMatrixReader {

            public static void main(String[] args) throws IOException{

            DataInputStream data_in = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("matlabFile.bin"))));

            float[][] t = new float[400][500];  //  A 2D array (or matrix) for floating numbers to be read.

            int y=0;  // initiation for the second dimension of the matrix

       

                 for (int x = 0;x<400;x++){

                    //  x is the first dimension of the matrix

                     try{

                           t[x][y] = data_in.readFloat(); // Reading process

                           System.out.println(t[x][y]);   // Printing process. The values of the numbers are about -7.1090133E34, 2.3835522E-26, and......

                                                                    // They have to be in the range of 10^-6 and 10^6

                                                                    // They are Wrong! and Why?

                     }

       

                     catch (java.io.EOFException eof) {

                           break;

                     }

       

                     if (x == 399) {

                             x = 0;

                             y++;

                     }  // New row initiation of X-axis

       

                     if(y==499){

                          System.out.println("File is read completely!");

                          data_in.close();

                     }  // Finish after the last element of the array is filled.

                }       

           }

      }