0

I'm having some trouble implementing the following MATLAB code in python.

for i = 1:N
    if (feof(fid) ~= 1)
        for j = 1:K
            if (feof(fid) ~= 1)
                    tmp = fread(fid, 1, 'float');
                    data.(fldnames{j,1}).Samples(i) = tmp;
            else
                disp('Error: End of file reached');
            end
        end
    else
        disp('Error: End of file reached');
    end

This is my 'like-for-like' python attempt,

sampleList = numpy.zeros((N, K))
for i in range(1, N) :
    for j in range(1, K) : 
        if EOF == 0 :
            try : 
                samples = array.array("f")
                samples.fromfile(f, 1)
                sampleList[i, j] = samples[0]
            except : 
                EOF = 1
return sampleList 

But I'm having trouble catching both of the EOFs. Is there an easier, more pythonic way of writing this code?

1
  • where does EOF come from? Commented Aug 14, 2014 at 7:39

2 Answers 2

1

This is what the matlab code does in python:

def read_data(filename, fieldnames, n):
    data = numpy.fromfile(filename, count=n * len(fieldnames))
    assert len(data) == n * len(fieldnames)
    return dict((key, data[idx::len(fieldnames)]) for idx, key in enumerate(fieldnames))

I assume that the number of field names is K and that fid is a filehandle to the file filename, and that n is the number of items per field N. What you get is a dictionary with fieldnames, which is equivalent to a struct in matlab.

Sign up to request clarification or add additional context in comments.

4 Comments

That looks great, thanks. Definitely better than just an NxK array.
Just trying this now, the dictionary this function returns seems to only have 5 elements (len(myDict["data_1"]) == 5. This should definitely return a dict of fieldnames, each one with n elements? What could be causing it to only have 5 elements? Your assumptions are correct.
look at the length of data. If there are not enough numbers in the file, then the data-Vector is too short.
would just like to add I fixed this, data was not long enough as you said, the cause was me not opening the binary file with the extra "rb" argument, so all of the file wasn't being read
1

Not sure where some of your values are coming from but if you want to catch the Exception:

        for j in range(1, K) :
            try :
                samples = array.array("f")
                samples.fromfile(f, 1)
                sampleList[i, j] = samples[0]
            except EOFError: # catch eof exception
                print ('Error: End of file reached')

1 Comment

thanks, I didn't know you could catch the EOFError explicitly like that. My EOF was just a flag, I don't think it was doing anything useful, I was trying to break out of the loop if I get EOF. I've since realized that my problem is somewhere else, as I continuously catch the exception when I'm reading the file, which doesn't happen in MATLAB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.