aldanor / hdf5-rust

HDF5 for Rust
https://docs.rs/hdf5
Apache License 2.0
310 stars 85 forks source link

How to write 2D array Vec<Vec<T>> #119

Closed Timmmm closed 4 years ago

Timmmm commented 4 years ago

This might be a dumb question, but if I have a Vec<Vec<u32>> (and I know it isn't jagged) how do I write it to a new dataset?

Something like this, but actually working:

let data: Vec<Vec<u32>> = ...;

file.new_dataset::<u32>().create("data", (data.len(), data[0].len()))?
      .write(&data)?;
magnusuMET commented 4 years ago

Vec<Vec<T>> is a really awkward datastructure, and does not currently have any direct conversion to a hdf5 datatype. Is there any way for you to read into an ndarray or use the natively understood hdf5-types types?

magnusuMET commented 4 years ago

If changing your datatype is not possible you can use write_slice and manually write each slice into the dataset.

aldanor commented 4 years ago

Concurring with what @magnusuMET said, but also worth mentioning #108 since this is yet another example of it – if something like that is implemented, it would be absolutely possible to both read and write things like Vec<Vec<Vec<String>>> etc (it's the read part that's complicated, but the write part is actually quite simple).

Timmmm commented 4 years ago

Thanks for the answers. Looks like write_slice is the way to go for now.