开发者

export matlab variable to text for python usage

So let's start off by saying I'm a total beginner in matlab. I'm working with python and now I've recieved some data in a matlab file that I need to export to a 开发者_开发百科format I could use with python.

I've googled around and found I can export a matlab variable to a text file using:

dlmwrite('my_text', MyVariable, 'delimiter' , ',');

Now the variable I need to export is a 16000 x 4000 matrix of doubles of the form 0.006747668446927. Now here is where the problem starts. I need to export the full values for each double. Trying with that function lead me to export the numbers in a format of 0.0067477. This won't do since I need a whole lot more of precision for what I'm doing. So how can I export the full values of each of these variables? Or if you have a more elegant way of using that huge matlab matrix in python please feel free.

Regards, Bogdan


To exchange big chunks of numerical data between Python and Matlab I recommend HDF5

  • http://en.wikipedia.org/wiki/Hierarchical_Data_Format

The Python binding is called h5py

  • http://code.google.com/p/h5py

Here are two examples for both directions. First from Matlab to Python

% matlab
points = [1 2 3 ; 4 5 6 ; 7 8 9 ; 10 11 12 ];
hdf5write('test.h5', '/Points', points);

# python
import h5py
with h5py.File('test.h5', 'r') as f:
    points = f['/Points'].value    

And now from Python to Matlab

# python
import h5py
import numpy
points = numpy.array([ [1., 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ])
with h5py.File('test.h5', 'w') as f:
    f['/Points'] = points

% matlab
points = hdf5read('test.h5', '/Points');

NOTE A column in Matlab will come out as a row in Python and vice versa. This isn't a bug but the difference between the way C and Fortran interpret a continuous piece of data in memory.


Scipy has tools for reading MATLAB .mat files natively: see e.g. http://www.janeriksolem.net/2009/05/reading-and-writing-mat-files-with.html


While I like the hdf5 based answer, I still think text files and CSVs are nice for smaller things (you can open them in text editors, spreadsheets whatever). In that case I would use MATLABs fopen/fprintf/fclose rather than dlmwrite - I like to make things explicit. Then again, this dlmwrite might be better for multi-dimensional arrays.


You can simply write your variable to file as binary data, then read it in any language you want, be it MATLAB, Python, C, etc.. Example:

MATLAB (write)

X = rand([100 1],'single');

fid = fopen('file.bin', 'wb');
count = fwrite(fid, X, 'single');
fclose(fid);

MATLAB (read)

fid = fopen('file.bin', 'rb');
data = fread(fid, Inf, 'single=>single');
fclose(fid);

Python (read)

import struct

data = []
f = open("file.bin", "rb")
try:
    # read 4 bytes at a time (float)
    bytes = f.read(4)           # returns a sequence of bytes as a string
    while bytes != "":
        # string byte-sequence to float
        num = struct.unpack('f',bytes)[0]

        # append to list
        data.append(num);

        # read next 4 bytes
        bytes = f.read(4)
finally:
    f.close()

# print list
print data

C (read)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp = fopen("file.bin", "rb");

    // Determine size of file
    fseek(fp, 0, SEEK_END);
    long int lsize = ftell(fp);
    rewind(fp);

    // Allocate memory, and read file
    float *numbers = (float*) malloc(lsize);
    size_t count = fread(numbers, 1, lsize, fp);
    fclose(fp);

    // print data
    int i;
    int numFloats = lsize / sizeof(float);
    for (i=0; i<numFloats; i+=1) {
        printf("%g\n", numbers[i]);
    }

    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜