function varargout = subscript(m,range,wantc) <m> is a matrix (of numbers) or a string referring to a matrix variable in the base workspace. cell matrices are okay. <range> is: (1) vector index range that does not use the 'end' keyword, e.g., 4, [5 6] (2) a logical indexing matrix (3) the string ':' (4) a cell vector where elements can be of types (1), (2), (3), e.g., {':' 4:5} <wantc> (optional) is whether to perform cell de-referencing. default: 0. return something like m(range), or m{range} when <wantc>. this function is useful for cases where you want to get a range of elements from something that already has parentheses or brackets. it is also useful for working with string-variable representations of matrices. it is also useful for writing functions that make no assumption about the size of the matrices used as input. example: isequal(size(subscript(randn(10,10),{1:2 ':'})),[2 10]) a = [1 2; 3 4]; subscript('a',1) subscript('a',a<1.5)
0001 function varargout = subscript(m,range,wantc) 0002 0003 % function varargout = subscript(m,range,wantc) 0004 % 0005 % <m> is a matrix (of numbers) or a string referring to a matrix 0006 % variable in the base workspace. cell matrices are okay. 0007 % <range> is: 0008 % (1) vector index range that does not use 0009 % the 'end' keyword, e.g., 4, [5 6] 0010 % (2) a logical indexing matrix 0011 % (3) the string ':' 0012 % (4) a cell vector where elements can be of types (1), (2), (3), 0013 % e.g., {':' 4:5} 0014 % <wantc> (optional) is whether to perform cell de-referencing. default: 0. 0015 % 0016 % return something like m(range), or m{range} when <wantc>. 0017 % 0018 % this function is useful for cases where you want 0019 % to get a range of elements from something that 0020 % already has parentheses or brackets. 0021 % it is also useful for working with string-variable 0022 % representations of matrices. it is also useful 0023 % for writing functions that make no assumption about 0024 % the size of the matrices used as input. 0025 % 0026 % example: 0027 % isequal(size(subscript(randn(10,10),{1:2 ':'})),[2 10]) 0028 % a = [1 2; 3 4]; 0029 % subscript('a',1) 0030 % subscript('a',a<1.5) 0031 0032 % input 0033 if ~exist('wantc','var') || isempty(wantc) 0034 wantc = 0; 0035 end 0036 0037 % do it 0038 if wantc 0039 if iscell(range) 0040 if ischar(m) 0041 varargout = evalin('base',['subscript(',m,',',cell2str(range),',1);']); 0042 else 0043 varargout = m(range{:}); 0044 end 0045 else 0046 if ischar(m) 0047 if ischar(range) && isequal(range,':') 0048 varargout = evalin('base',[m,'(:);']); 0049 else 0050 varargout = evalin('base',[m,'(',mat2str(range),');']); 0051 end 0052 else 0053 if ischar(range) && isequal(range,':') 0054 varargout = m(:); 0055 else 0056 varargout = m(range); 0057 end 0058 end 0059 end 0060 else 0061 varargout = cell(1,1); 0062 if iscell(range) 0063 if ischar(m) 0064 varargout{1} = evalin('base',['subscript(',m,',',cell2str(range),');']); 0065 else 0066 varargout{1} = m(range{:}); 0067 end 0068 else 0069 if ischar(m) 0070 if ischar(range) && isequal(range,':') 0071 varargout{1} = evalin('base',[m,'(:);']); 0072 else 0073 varargout{1} = evalin('base',[m,'(',mat2str(range),');']); 0074 end 0075 else 0076 if ischar(range) && isequal(range,':') 0077 varargout{1} = m(:); 0078 else 0079 varargout{1} = m(range); 0080 end 0081 end 0082 end 0083 end