lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


A question regarding lpack:

 I'm trying to read a MATLAB version 4 MAT-file [1]. It is decomposed into
 five four-byte headers, a variable name and a value assigned to the
 variable name. One of the headers indicates that the values stored are
 double-precision (64-bit) floating point numbers. I am having issues with
 the parsing of the data.

 My mat file looks like this in a hex editor:

 ,----[ hex ]-
 | 0000000: 0000 0000 0100 0000 0100 0000 0000 0000  ................
 | 0000010: 0400 0000 6b61 7000 0000 0000 0000 2e40  ....kap........@
 | 0000020: 0d0a
 `----

 My lua code to read this follows:
 ,----[ lua code ]-
 | block = f:read(4)
 | local mopt = tonumber(string.byte(block))
 | print(string.format('type(MOPT) = %04d', mopt))
 | print(string.format('type(MOPT_unpack) = %04d', mopt_unpack))
 |
 | block = f:read(4)
 | local mrows = tonumber(string.byte(block))
 | print(string.format('mrows = %d',mrows ))
 |
 | block = f:read(4)
 | local ncols = tonumber(string.byte(block))
 | print(string.format('ncols = %d', ncols))
 |
 | block = f:read(4)
 | local imagf = tonumber(string.byte(block))
 | print(string.format('imagf = %d', imagf))
 |
 | block = f:read(4)
 | local namelen = tonumber(string.byte(block))
 | print(string.format('namelen = %d', namelen))
 |
 | block = f:read(namelen)
 | local varname = block
 | print('variable name is ' .. varname)
 |
 | for i = 1, ncols do
 |   block = string.unpack('n',f:read(8))
 |   print('value is ' .. block)
 |   --print(string.format('value %i = %4d',i,tonumber(string.byte(block))))
 | end
 `----

 The file read for value is 8 bytes, which I counted from the hex file
 (very scientific on my part, although MATLAB reports the variable kap to
 be 8 bytes as well). It ought to report that value is 15. I've tried using
 various operators, including f, d, n, etc., but I can't get the right
 value out, as seen in the output below.

 Additionally, when I use string.unpack on the five long (4-byte) integer
 headers, I get results that don't match what I would expect. A sample is
 shown in the output below.

 What am I doing wrong????


 My output follows:
 ,----[ output ]-
 | type(MOPT) = 0000
 | type(MOPT_unpack) = 0001
 | mrows = 1
 | ncols = 1
 | imagf = 0
 | namelen = 4
 | variable name is kap
 | value is 1
 `----

 [1] The MAT-file specification:
http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf

 Thanks for any input,
 Keith Pimmel
 --
 Keith   ~~~~    pimmel@ieee.org