-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist_elem.m
47 lines (42 loc) · 1.15 KB
/
list_elem.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
function out = list_elem(rec,k,def)
% LIST_ELEM - Return the k-th element of a list if it exists, otherwise return a default value.
%
% This function retrieves the k-th element from the input list 'rec'. If the k-th element does not
% exist, it returns the specified default value 'def'.
%
% Syntax:
% out = list_elem(rec,k,def)
%
% Inputs:
% rec - List or cell array from which to retrieve the element.
% k - Integer index specifying which element to retrieve.
% def - Default value to return if the k-th element does not exist.
%
% Outputs:
% out - The k-th element of the input list if it exists, otherwise the default value 'def'.
%
% Example:
% rec = {'a', 'b', 'c'};
% k = 2;
% def = 'default';
% result = list_elem(rec, k, def); % Returns 'b'
%
% Input validation
if (nargin < 2)
error('Argument Mismatch - Check Command Line');
end
if ~exist('def', 'var')
def = [];
end
if ~iscell(rec)
rec = {rec};
end
if (~isnumeric(k)) || (rem(k,1) ~= 0) || (k <= 0)
error('''k'' must be a strictly positive integer');
end
% Retrieve the k-th element or default value
if (k <= length(rec))
out = rec{k};
else
out = def;
end