The String example is straightforward because UTF-8 already specifies how a bunch bytes (UInt8) should be interpreted to become a string. For a Matrix, such a straight conversion cannot work because the dimension information about the matrix isn’t stored anywhere, and is necessary to create it.
You can get the data as a vector of UInts first with reinterpret:
It’s likely the wrong approach, it only stores the values, not dimensions here 20, 20, so it might as well be a vector of 400. Only do this in very few cases, where you know the dimensions of the data to be static (in case you want to write more to the file). What you likely want is some kind of serialization system, there are many, but this one works, and I think it may be the best one:
Well neither is it straightforward (if you want to store more than one string in a file), because I checked and it stores the String exactly, in my test “Palli”, my name as 5 bytes, not even a trailing \0 or length, so write, and its docs are rather useless and misleading. I feel it’s missing a note, stating most should rather use JLD2.jl (and some software, likely it, might use write indirectly for you).
That would be a good reason to avoid it then? But the situation is a bit more complex though:
The data format can change in minor (1.x) Julia releases, but files written by prior 1.x versions will remain readable. The main exception to this is when the definition of a type in an external package changes. If that occurs, it may be necessary to specify an explicit compatible version of the affected package in your environment. Renaming functions, even private functions, inside packages can also put existing files out of sync. Anonymous functions require special care: because their names are automatically generated, minor code changes can cause them to be renamed. Serializing anonymous functions should be avoided
I don’t recall the pros and cons of all the options, should JLD2.jl me promoted over this?