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.

}

}

}

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.

Quote:

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.

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

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.

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

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

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

Quote:

Originally Posted by

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