Skip to content

Commit b50dd02

Browse files
committed
[Matlab] Add nix.Utils.handle_index
1 parent b9b7442 commit b50dd02

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

+nix/Utils.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,26 @@
158158
end
159159

160160
% -----------------------------------------------------------
161-
% c++ to matlab array functions
161+
% c++ vs matlab helper functions
162162
% -----------------------------------------------------------
163163

164164
function r = transpose_array(data)
165165
% Data must agree with file & dimensions; see mkarray.cc(42)
166166
r = permute(data, length(size(data)):-1:1);
167167
end
168+
169+
function r = handle_index(idx)
170+
% Matlab uses 1-based indexing opposed to 0 based indexing in C++.
171+
% handle_index transforms a Matlab style index to a C++ style
172+
% index and raises the appropriate Matlab error in case of an
173+
% invalid subscript.
174+
if (idx < 1)
175+
err.identifier = 'MATLAB:badsubscript';
176+
err.message = 'Subscript indices must either be real positive integers or logicals.';
177+
error(err);
178+
end
179+
r = idx - 1;
180+
end
168181
end
169182

170183
end

0 commit comments

Comments
 (0)