panda011
panda011

Reputation: 1

How to split 'matfile' (which has 4 elements)? I want to save each element into separate list

I am trying to split 4 elements in 'xxx.mat' file and want to store each element into a separate list. In other words, I want (100,100) data and save into other list and then, I want to make image file (.bmp) by using that data (100,100).

xxx.mat file includes

type       size 
list         0
bytes        1
string       1
float64  (100,100)

I have tried

a = np.genfromtxt('xxx.mat', delimiter=',')

However, I have got the result as

[[nan nan ....... nan nan]
 [nan nan ....... nan nan]

I tried to compare the data by using the following code (I have converted MAT-file to CSV file)

mat_contents = sio.loadmat('xxx.mat')
a= np.genfromtxt('xxx.csv', delimiter=',')
[[nan nan ....... nan nan]
 [nan nan ....... nan nan]

Upvotes: 0

Views: 688

Answers (1)

Sebastian Dengler
Sebastian Dengler

Reputation: 1308

I'll share with you the solution i came up with:

  1. I created a .mat file from random data in matlab and saved it.
X = rand(100,100)
save("xxx.mat","X")
  1. Using scipy.ioand this answer i loaded the .mat file into my python workspace
import scipy.io

mat = scipy.io.loadmat('xxx.mat')
X = mat["X"]
  1. Using the PILlibrary to convert the data to a black and white .bmp
from PIL import Image

img = Image.fromarray(X, 'L')
img.save('my.bmp')
img.show()

enter image description here

Upvotes: 0

Related Questions