How to convert Matlab cell array into Python numpy array?
Show older comments
In matlab, I generate a random # of images of the shape (130,100) and save them one by one in a cell array. I then need to import this cell array of images into python and convert them into a numpy array (number_images, 130,100).
I've tried the following, but I get an error. Not sure how to fix. Would appreciate help- thanks!
import scipy.io as sio
import numpy
from PIL import Image
folder='insert path of mat file here'
imgs = sio.loadmat(folder+'/img_array.mat')
num_imgs=len(imgs['img_array'][0])
img_array=np.array(imgs['img_array'][0])
new_array=img_array.reshape((num_images,130,100))
ValueError: cannot reshape array of size 9 into shape (9,130,100)
**In this case, 9 images were generated from matlab, and this was the cell array shown.

Accepted Answer
More Answers (0)
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!