Skip to content

Doesn't support complex types in NPY files #9

@darkstar007

Description

@darkstar007

As the subject says!

Here is my quick hack to allow complex numpy files to be read:

--- a/npy-matlab/readNPYheader.m
+++ b/npy-matlab/readNPYheader.m
@@ -22,8 +22,8 @@ end
 
 try
     
-    dtypesMatlab = {'uint8','uint16','uint32','uint64','int8','int16','int32','int64','single','double', 'logical'};
-    dtypesNPY = {'u1', 'u2', 'u4', 'u8', 'i1', 'i2', 'i4', 'i8', 'f4', 'f8', 'b1'};
+    dtypesMatlab = {'uint8','uint16','uint32','uint64','int8','int16','int32','int64','single','double', 'logical', 'complex8', 'complex16'};
+    dtypesNPY = {'u1', 'u2', 'u4', 'u8', 'i1', 'i2', 'i4', 'i8', 'f4', 'f8', 'b1', 'c8', 'c16'};
     
--- a/npy-matlab/readNPY.m
+++ b/npy-matlab/readNPY.m
@@ -20,8 +20,16 @@ try
     [~] = fread(fid, totalHeaderLength, 'uint8');
 
     % read the data
-    data = fread(fid, prod(shape), [dataType '=>' dataType]);
-
+    if strcmp(dataType, "complex8") == 1
+       data = fread(fid, prod(shape)*2, 'single=>single');
+       data = data(1:2:end) + 1j * data(2:2:end);
+    elseif strcmp(dataType, "complex16") == 1
+       data = fread(fid, prod(shape)*2, 'double=>double');
+       data = data(1:2:end) + 1j * data(2:2:end);
+    else
+       data = fread(fid, prod(shape), [dataType '=>' dataType]);
+    end
+    

The above seems to work, but not very well tested.

Thanks,

Mat

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions