Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: I can't transfer floating number Matrix(400x500) from MATLAB bin file to a Java Array

  1. #1
    Sarp is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default I can't transfer floating number Matrix(400x500) from MATLAB bin file to a Java Array

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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: I can't transfer floating number Matrix(400x500) from MATLAB bin file to a Java A

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    values are completely different than they are saved in MATLAB
    Do you mean that in MATLAB a number is 6.789 and after the java program does the conversion it is 234.445
    In other words the conversion of the numbers is completely wrong.

    Can MATLAB write out one or two numbers to a file that could be examined in a hex editor to see their binary values so a java program could be written to convert MATLAB output to valid java numbers.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,380
    Rep Power
    5

    Default Re: I can't transfer floating number Matrix(400x500) from MATLAB bin file to a Java A

    I am unfamiliar with MatLab but it took me about 5 minutes to determine there are variety of import/export facilities as well as quite a few supported file types. It even shows how to integrate some capabilities with Java. Did you check out the documentation?

    Regards,
    Jim
    Norm likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Sarp is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: I can't transfer floating number Matrix(400x500) from MATLAB bin file to a Java A

    Yes, be cause there are websites showing how to pass from a matlab.bin file to read them in Java. Such as float matrix in a matlab.bin file must be saved with '-singe' parameter to be shown in Java as 'float'. So a 2D matrice must be readable with java.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,380
    Rep Power
    5

    Default Re: I can't transfer floating number Matrix(400x500) from MATLAB bin file to a Java A

    Well, I suggest you start off small. Go to MatLab and create a small 1 x 2 array of floats and save them. Then you have several options.

    1. Do what Norm suggested and analyze the file with a bin hex editor.
    2. Create a 1 x 2 array of floats in java (of the same values of course) and use a DataStream
    to write them out and compare to the MatLab version.
    3. Find another way to write them to a different and perhaps easier to parse/verify file format.

    I presume that MatLab is storing this in IEEE 754 format which means it must be read in as a float and not text. And when you analyze it
    it won't look anything like a text floating point number.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Sarp is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: I can't transfer floating number Matrix(400x500) from MATLAB bin file to a Java A

    Dear Jim,
    I was graduated from Bilkent University at 2013. I am finished with some books and produced considerable projects. The issue we are talking right now is an issue of year 2009 on another forum in the internet and someone tried to clear up but could not finished to give a sense. Do you want me to share the website with you?
    The code upside is the most compact form of it with just 2 parameters of a table as X=400 and Y=500. If the object types are incompatible or if you have another solution let me have your kind response.
    Yours sincerely,
    Sarp

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,380
    Rep Power
    5

    Default Re: I can't transfer floating number Matrix(400x500) from MATLAB bin file to a Java A

    Quote Originally Posted by Sarp View Post
    Dear Jim,
    I was graduated from Bilkent University at 2013. I am finished with some books and produced considerable projects. The issue we are talking right now is an issue of year 2009 on another forum in the internet and someone tried to clear up but could not finished to give a sense. Do you want me to share the website with you?
    The code upside is the most compact form of it with just 2 parameters of a table as X=400 and Y=500. If the object types are incompatible or if you have another solution let me have your kind response.
    Yours sincerely,
    Sarp
    I am not certain what this has to do with my recommendation but that is all I have to offer. And no thanks! I don't want the website as I will not visit posted web sites on this forum until they have been vetted or I am familiar with the poster.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. How to round up floating number properly?
    By IndraG in forum New To Java
    Replies: 4
    Last Post: 01-19-2012, 07:57 AM
  2. Java array matrix problem.. please help!
    By Asvin in forum New To Java
    Replies: 5
    Last Post: 04-11-2011, 07:35 PM
  3. if it is floating point number
    By Stephen Douglas in forum New To Java
    Replies: 26
    Last Post: 07-06-2010, 09:17 AM
  4. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  5. number of floating point
    By mohammad8065 in forum Advanced Java
    Replies: 5
    Last Post: 12-28-2008, 09:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •