0

I create matrices of data in Matlab which I want to use in my java project (Android). I could put it in a file and read it at runtime. But since the data is constant, I wonder if there isn't a better way to integrate it with the source code.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
LocalFluff
  • 111
  • 7

1 Answers1

0

The clearest way to do that would be to put your matrices in the appropriate format (MathML or TeX maybe) into the assets folder of your Android project [goes right next to src, res and others], then read the file from the app like this:

...
public void onCreate(Bundle savedInstanceState) {
    ...

    InputStream iStream = getAssets().open("myMatrices.tex");
    // Then pass the iStream to whatever parser you choose
}

Here's a good read on mathematical parsers for Java: What's a good library for parsing mathematical expressions in java?

Hardcoding your matrices as constants in your classes is fine too, although less flexible. So, if you already know all the matrices you'll need, go with hardcoding, this will shave some time on parsing and a bit of .apk size [due to not having to include any 3rd party parser libraries]

Community
  • 1
  • 1
Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59
  • Would I gain something by declaring it as a constant in the source code of the class where it will be needed? – LocalFluff Feb 18 '14 at 12:30
  • Well, if it's already in a format you can use right away for calculations/display -- yes, you'd save some time not having to parse the asset file. The speedup won't be very noticeabe but it will be there. Using an asset file on the other hand will possibly give you more flexibility -- but it depends on the task at hand. If you plan on putting in hundreds of matrices it won't be very convenient hardcoding them all, you better go with the file and parser thing. If your task will take 10 matrices and there's no intention to ever add many more -- you're fine with hardcoded matrices. – Ivan Bartsov Feb 18 '14 at 12:39