What language feature or outside-the-box hack can I use to accomplish function inlining in MATLAB? Annoyingly, a Google search for "matlab inline function" reveals that MATLAB's designers thought that "to inline" means "to construct an anonymous function out of a string" (...wtf?).
The reason I ask is I am writing a script that needs to run fast, and I am encountering a lot of situations where it would be nice to have a helper function handle some simple addition or something to avoid off-by-one errors. However, the cost of a function call (as measured by tic/toc
) would not be worth it. I refuse to believe that MATLAB would lack a function inlining feature because that would discourage decomposition!
Worst case, I could resort to using M4 macros.
EDIT: In response to eat's comment, here is a situation where I might like to inline a helper:
At one point, I need to loop (yeah, I know) over all windows of width windowWidth
within a list:
lastWindowStartIdx = length(list) - windowWidth + 1;
for windowStartIdx = 1:lastWindowStartIdx
display(list[windowStartIdx:windowStartIdx+windowWidth-1]); %the window we're looking at
end
It would be nice to be able to factor out those tricky, off-by-one-error-prone windowWidth
calculations. E.g.:
lastWindowStartIdx = calcLastWindowStartIdx(length(list), windowWidth);
for windowStartIdx = 1:lastWindowStartIdx
display(list[windowStartIdx:calcWindowEndIdx(windowStartIdx, windowWidth)]); %the window we're looking at
end