1

I'm trying to work on the UJMP library. I created a matrix and I'd like to export it in a txt file and then to import it again as a basic Matrix. I tried the following code but it doesn't work:

Matrix m = MatrixFactory.dense(ValueType.INT, new long[]{(long)100,(long)100});
for(int i = 0; i< 100; i++){
        for(int j = 0; j< 100;j++){
            m.setAsInt((int) (Math.random() *3),new long[]{i,j});
        }
}
try {
        m.exportToFile(FileFormat.TXT,"test.txt",null); //this works. My file contains the matrix
} catch (MatrixException e) {
        e.printStackTrace();
} catch (IOException e) {
        e.printStackTrace();
}
Matrix n = null;
try {
        n =ImportMatrix.fromFile(FileFormat.TXT,new File("test.txt"),null); //this doesn't work. The matrix has a size of 1*1 and contains the content of the previous matrix as a single string.
} catch (MatrixException e) {
        e.printStackTrace();
} catch (IOException e) {
        e.printStackTrace();
}

System.out.println(n.getSize()[0]+" "+n.getSize()[1]); // I get 1 1;

So what should I do to get my original matrix as a 100*100 matrix ?

Thank you in advance for your response, and sorry for my english.

demongolem
  • 9,474
  • 36
  • 90
  • 105
criardialo
  • 11
  • 2

2 Answers2

0

I'm not sure but I think you use null as delimiter in your file :

m.exportToFile(FileFormat.TXT,"test.txt",null);

Maybe you should use :

m.exportToFile(FileFormat.TXT,"test.txt",";");
AlexB
  • 7,302
  • 12
  • 56
  • 74
noob
  • 1
0

Actually you can export matrix to file using method

exportTo()

and choose the destination

For example:

m.exportTo().file(file)

and then choose the export format

For example:

m.exportTo().file(file).asMatlabScript("myMat");
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
  • Thank you for the answer. This is probably right. I'm not sure how I finally circumveted this issue but I don't have the project anymore so I can't test it to verify. – criardialo Sep 09 '16 at 04:47