-
Notifications
You must be signed in to change notification settings - Fork 0
/
readmda.m
82 lines (73 loc) · 1.46 KB
/
readmda.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function A=readmda(fname)
%READMDA - read the contents of a .mda file. MDA stands for
%multi-dimensional array.
%
% See http://magland.github.io//articles/mda-format/
%
% Syntax: A=readmda(fname)
%
% Inputs:
% fname - path to the .mda file
%
% Outputs:
% A - the multi-dimensional array
%
% Other m-files required: none
%
% See also: writemda
% Author: Jeremy Magland
% Jan 2015; Last revision: 15-Feb-2106
if (strcmp(fname(end-4:end),'.csv')==1)
A=textread(fname,'','delimiter',',');
return;
end
F=fopen(fname,'rb');
try
code=fread(F,1,'int32');
catch
error('Problem reading file: %s',fname);
end
if (code>0)
num_dims=code;
code=-1;
else
fread(F,1,'int32');
num_dims=fread(F,1,'int32');
end;
dim_type_str='int32';
if (num_dims<0)
num_dims=-num_dims;
dim_type_str='int64';
end;
S=zeros(1,num_dims);
for j=1:num_dims
S(j)=fread(F,1,dim_type_str);
end;
N=prod(S);
if num_dims == 1,
A = zeros(1,S);
else
A=zeros(S);
end
if (code==-1)
M=zeros(1,N*2);
M(:)=fread(F,N*2,'float');
A(:)=M(1:2:prod(S)*2)+i*M(2:2:prod(S)*2);
elseif (code==-2)
A(:)=fread(F,N,'uchar');
elseif (code==-3)
A(:)=fread(F,N,'float');
elseif (code==-4)
A(:)=fread(F,N,'int16');
elseif (code==-5)
A(:)=fread(F,N,'int32');
elseif (code==-6)
A(:)=fread(F,N,'uint16');
elseif (code==-7)
A(:)=fread(F,N,'double');
elseif (code==-8)
A(:)=fread(F,N,'uint32');
else
error('Unsupported data type code: %d',code);
end;
fclose(F);