Search code examples
javaappendmat-file

appending mat file in java


I have a question about the com.jmatio.io package that I was hoping someone could answer. I am looking to write to a .mat file (using java) that may or may not already exist. If it exists I would like to append the information to the end but if the file is not created I would like to create a new file and just add the contents to that. My second write is overwriting the first but I would not like it to do this. Any suggestions or solutions is gladly appreciated.


Solution

  • If you want to write multiple arrays to a new file you can achieve it using the MatFileIncrementalWriter. As it's explained in it's javadoc

    An updated writer which allows adding variables incrementally for the life of the writer. This is necessary to allow large variables to be written without having to hold onto then longer than is necessary.

    And it states clearly that you can't append to an existing file.

    If you want to append to an existing file you might need

    • read variables from the existing file
    • write the existing variables back to the file using a MatFileIncrementalWriter
    • add new variables to the incremental writer