-
Notifications
You must be signed in to change notification settings - Fork 0
/
isTruthy.m
116 lines (99 loc) · 2.33 KB
/
isTruthy.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function out = isTruthy(in)
% Check if the input is truthy
% Check input type
% All the basic MATLAB Types are here:
% https://mathworks.com/help/matlab/data-types.html
% NOTE: <boolean>
if isequal(in, false) || isequal(in, true)
out = in;
return
end
if islogical(in)
out = all(in(:));
return
end
% NOTE: empty (number | char) array | empty cell | empty table
% | empty categorical | empty timetable | empty containners.Map
% | empty dataset
if isempty(in)
out = false;
return
end
% NOTE: <non-empty table>
if istable(in)
out = true;
return
end
% NOTE: <non-empty cell>
if iscell(in)
if iscellstr(in)
in = string(in);
else
out = true;
return
end
end
% NOTE: <non-empty char-array>
if ischar(in)
out = true;
return
end
% NOTE: <string>
if isstring(in)
out = strlength(in) > 0;
return
end
% NOTE: <struct>
if isstruct(in)
out = ~isempty(fieldnames(in));
return
end
% NOTE: <graphics handle>
if ishghandle(in)
% NOTE: `isgraphics` tests validity of graphics handle.
out = true;
return
end
% NOTE: <enum>
if isenum(in)
out = true;
return
end
% NOTE: <duration>
if isduration(in)
% NOTE: Calling `duration` with no parameters instantiates
% the "00:00:00" duration.
out = all(in ~= 0);
return
end
% NOTE: <non-empty timetable>
if istimetable(in)
out = true;
return
end
% NOTE: <timeseries>
if isa(in, 'timeseries')
% NOTE: <empty timeseries> has
% non-empty `TimeInfo` and `DataInfo` properties.
out = ~(timeseries == in);
return
end
% NOTE: <datetime>
if isdatetime(in)
% NOTE: `datetime` is identical to `datetime('now')`.
out = ~any(in.isnat());
return
end
% NOTE: nan
if isnumeric(in) && any(isnan(in(:)))
out = false;
return
end
% NOTE: <function handle>
if isa(in, 'function_handle')
out = true;
return
end
% NOTE: <number array> | <calendar> | <unknown type>
out = all(logical(in(:)));
end