-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst.m
More file actions
26 lines (22 loc) · 667 Bytes
/
first.m
File metadata and controls
26 lines (22 loc) · 667 Bytes
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
function elem = first(coll)
%FIRST Returns the first element of any collection, or 'nil' if empty
% elem = FIRST(coll) returns the first element of any vector, array, or
% collection. If 'coll' is empty, nil is returned (see nil function).
%
% This aids one-liners (e.g. if a function returns an array, and you want
% only the first element, all in one line), and generality (get the first
% element of a standard array or cell with the same syntax, instead of
% (1) or {1}).
%
% Part of Burgbox
% 2013-02 CB created
if isNil(coll)
elem = coll;
elseif isempty(coll)
elem = nil;
elseif iscell(coll)
elem = coll{1};
else
elem = coll(1);
end
end