4

I'm measuring the runtime of my system with an ArrayList<Long> runtime. The different entries of the list I'm getting through System.nanoTime(); which returns a long.

I'm now writing this ArrayList to a text file by just looping over the list and using Long.toString(); for each entry.

So far so good, but now I also create a .mat file with JMatIO (Matlab file format). I do this in the following way (remember ArrayList<Long> runtime):

// Convert ArrayList to array because JMatIO only accepts arrays
long[] runtimeArr = new long[runtime.size()];
int i = 0;
for (long a : runtime) {
   runtimeArr[i] = a;
   i++;
}

// Write to .mat file.
ArrayList<MLArray> list = new ArrayList<MLArray>();
MLInt64 m = new MLInt64("runtime", runtimeArr, 1);
list.add(m);
new MatFileWriter("runtime.mat", list); // Creates .mat

If I open the .mat file in Matlab the displayed numbers are slightly different than in the text file (I'm using format long g in Matlab). For example in Matlab I'm getting 1988336630 and in the text file 1993496993.

Why is this?

Edit: Here is a complete example

public class Main {
  public static void main(String[] args) {
     ArrayList<Long> runtime = new ArrayList<Long>();
     Thread.sleep(2000);
     runtime.add(System.nanoTime());
     File file = new File("filename.txt");
     FileWriter fw = new FileWriter(file.getAbsoluteFile());
     BufferedWriter bw = new BufferedWriter(fw);
     bw.write(Long.toString(runtime.get(0)));
     bw.close(); 

     // Convert ArrayList to array because JMatIO only accepts arrays
     long[] runtimeArr = new long[runtime.size()];
     int i = 0;
     for (long a : runtime) {
        runtimeArr[i] = a;
        i++;
     }

     // Write to .mat file.
     ArrayList<MLArray> list = new ArrayList<MLArray>();
     MLInt64 m = new MLInt64("runtime", runtimeArr, 1);
     list.add(m);
     new MatFileWriter("runtime.mat", list); // Creates .mat
  }
}
machinery
  • 5,972
  • 12
  • 67
  • 118
  • The error would exactly match the precision error of single variables: `single(1988336630)==single(1988336639)` is true. What data type does that code really write? – Daniel Feb 05 '16 at 17:15
  • @Daniel So the ArrayList contains Long and these are also written to the text file (Long.toString()). For writing the .mat file I convert the ArrayList to long[] which should be fine I think.This I pass to the JMatIO library. – machinery Feb 05 '16 at 17:35
  • @Daniel I have just seen that the JMatIO library first does a casting from long[] to Long[] but I don't think that this should be the problem. – machinery Feb 05 '16 at 17:39
  • 1
    @Daniel If I make "whos runtime" in Matlab I see that the runtime variable in Matlab is indeed int64. So this should be ok. By the way, By the way, other value examples are 1004225605 (java) vs. 1000584651 (Matlab) and 40294 (Java) vs. 12544 (Matlab). – machinery Feb 05 '16 at 19:07
  • Did you try to further simplify your code? Just some constants in your java code instead of text parsing. At this point I think there is some bug in the library, and kicking out the text parsing stuff would make a perfect code example for a bug report. – Daniel Feb 05 '16 at 22:29
  • Post the code you use to load it in matlab. – Mad Physicist Jul 07 '16 at 06:17

0 Answers0