1

I want to save a cell of 1 x 2000 of matrices 20 x 500 done in matlab in a h5 file and then load it in python. I couldn't save it in matlab. Is there any solutions for this ? Here is the code I use to save but it doesn't work with cells.

for i = 1:length(idxtrain)
x_train{i} = image_array_prec{idxtrain(i)}(:,:,1);
end

for i = 1:length(idxtest)
x_test{i} = image_array_prec{idxtest(i)}(:,:,1);
end
%%
savefileh5 = 'traintest.h5'
%%
try
  h5create(savefileh5,'/train/x_train',[size(x_train,1) size(x_train,2)]);
  h5write(savefileh5,'/train/x_train',x_train);
  catch ME
  warning('File already in folder');
end

%%
x_train1 = h5read(savefileh5,'/train/x_train');
  • "I couldn't save it in matlab". Why? Did matlab decide not to save it? is your code wrong? did the computer solve quantum cryptography instead?! :) Jokes aside, I hope you get the idea. "It doesnt work" is not really any new information, if it did you would not be asking here! Please care to share more information? – Ander Biguri May 07 '21 at 09:17
  • Note that `.mat` files are a wrapped `hdf5` format and that they can be easily read in python. Plenty of info about that in this page and internet. – Ander Biguri May 07 '21 at 09:17
  • I know how to save in a h5 file in matlab but I don't know how to save cells. – Georges Murr May 07 '21 at 09:26
  • please show a [mcve] – Ander Biguri May 07 '21 at 09:28
  • for i = 1:length(idxtrain) x_train{i} = image_array_prec{idxtrain(i)}(:,:,1); end for i = 1:length(idxtest) x_test{i} = image_array_prec{idxtest(i)}(:,:,1); end %% savefileh5 = 'traintest.h5' %% try h5create(savefileh5,'/train/x_train',[size(x_train,1) size(x_train,2)]); h5write(savefileh5,'/train/x_train',x_train) end x_train1 = h5read(savefileh5,'/train/x_train'); – Georges Murr May 07 '21 at 10:55
  • I usually do this to save in h5 file but it doesn't save the cell. – Georges Murr May 07 '21 at 10:56
  • please, [edit] the question with the new infromation – Ander Biguri May 07 '21 at 11:11
  • 1
    I added the code I use. – Georges Murr May 07 '21 at 11:25
  • According to the docs, the input to `h5write` can only be a numeric matrix or a string array. I suggest trying to do a for loop that writes each cell into `h5write` – Ander Biguri May 07 '21 at 12:30
  • I tried it doesn't work – Georges Murr May 07 '21 at 14:30
  • I can't help with Matlab. It looks like you are trying to write training images from `image_array_prec` to a HDF5 formatted file (and eventually do the same with test images). This is easily accomplished with h5py and opencv or pillow. Here are examples on SO: [Simple](https://stackoverflow.com/a/66823010/10462884) ; [Detailed](https://stackoverflow.com/a/66641176/10462884). You may also find this answer helpful: (https://stackoverflow.com/a/67185709/10462884) – kcw78 May 07 '21 at 14:36
  • thank you for your message I will try it, but I was avoiding using cv2 in python because it is not working on my computer, could you please suggest a way to make cv2 work, I tried to install it using: pip install opencv-python , but it gave an error. – Georges Murr May 07 '21 at 14:57
  • 1
    I think MATLAB `save` can handle `cell`, but the resulting `HDF5` file is rather complex, and harder to load with `h5py`. For older formats `scipy.io.loadmat` loads `cell` as an object dtype array, in effect, arrays within an array. – hpaulj May 07 '21 at 16:43

0 Answers0