/
ft_struct2single.m
86 lines (76 loc) · 2.85 KB
/
ft_struct2single.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
83
84
85
86
function [x] = ft_struct2single(x, maxdepth)
% FT_STRUCT2SINGLE converts all double precision numeric data in a structure
% into single precision, which takes up half the amount of memory compared
% to double precision. It will also convert plain matrices and cell-arrays.
%
% Use as
% x = ft_struct2single(x)
%
% Starting from MATLAB 7.0, you can use single precision data in your
% computations, i.e. you do not have to convert back to double precision.
%
% MATLAB version 6.5 and older only support single precision for storing
% data in memory or on disk, but do not allow computations on single
% precision data. After reading a single precision structure from file, you
% can convert it back with FT_STRUCT2DOUBLE.
%
% See also FT_STRUCT2DOUBLE, FT_STRUCT2CHAR, FT_STRUCT2STRING
% Copyright (C) 2005-2014, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
if nargin<2
maxdepth = inf;
end
% convert the data, work recursively through the complete structure
x = convert(x, 0, maxdepth);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% this subfunction does the actual work
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [a] = convert(a, depth, maxdepth)
if depth>maxdepth
ft_error('recursive depth exceeded');
end
switch class(a)
case 'struct'
% process all fields of the structure recursively
fna = fieldnames(a);
% process all elements of the array
for j=1:length(a(:))
% warning, this is a recursive call to traverse nested structures
for i=1:length(fna)
fn = fna{i};
ra = getfield(a(j), fn);
ra = convert(ra, depth+1, maxdepth);
a(j) = setfield(a(j), fn, ra);
end
end
case 'cell'
% process all elements of the cell-array recursively
% warning, this is a recursive call to traverse nested structures
for i=1:length(a(:))
a{i} = convert(a{i}, depth+1, maxdepth);
end
case {'double' 'int64' 'uint64' 'int32' 'uint32' 'int16' 'uint16' 'int8' 'uint8'}
% convert the values to single precision
if ~issparse(a)
a = single(a);
end
otherwise
% do nothing
end