calling a built-in private function

9 views (last 30 days)
Chad Greene
Chad Greene on 29 Aug 2022
Commented: dpb on 30 Aug 2022
I'm using stream2 for a big problem that takes about a week to run on a high-performance computer. The profiler tells me that more than half of the processing time is just overhead in the stream2 function. Just a few simple tweaks to the stream2 function could save me days of processing time, but stream2 calls stream3c, which is in a private folder. How do I tell my new stream2_fast function to call stream3c?
My new function looks something like this:
function xy = stream2_fast(...)
% My attempt to addpath, of course, is in vain:
addpath('/Applications/MATLAB_R2022a.app/toolbox/matlab/specgraph/private')
xy = cell(1,N);
parfor k = 1:N
xy{k} = stream3c([],[],[],vx,vy,[],xind(k),yind(k),[],step,maxvert)';
end
end
The addpath line above doesn't add the path to the private folder, so when I run stream2_fast I get this error message:
'stream3c' is not found in the current folder or on the MATLAB path, but exists in:
/Applications/MATLAB_R2022a.app/toolbox/matlab/specgraph/private
If I change directory to the private folder, the code works perfectly and is much faster than MATLAB's stream2. But is there a way to call stream3c without changing directory to the private folder?

Accepted Answer

dpb
dpb on 29 Aug 2022
private functions are unable to be called from anywhere excepting either the folder containing the function itself (not the intended use) or (intended) from within a function a level above the associated @private folder.
Your options would be
  1. Make a copy of the private function (and all its dependencies) and place in a location visible to your function,
  2. Place your modified function in the directory of stream2 so it would also have access to @private,
  3. Use cd to the location.
None of these are all that great of options; the cleanest even though generally not recommended to use cd to hop, skip and jump around, is probably actually doing that here. Most of the time we see it in an effort by new(er) users to avoid having to create and use fully-qualified filenames (FQN) for data files so they cd to the data folder instead. Here, you're running a long-running code and not straying (I presume?) from that location so that if you also use the practice of FQN's for your data files, it should work just fine.
2. above isn't that bad if you don't modify anything in the distributed folder/files but only add an additional function to it -- again, not recommended as general practice -- but, unlike making user modifications to distributed functions for special purposes, being unique and new, it would not be overwritten by a new install as would such other user changes.
Others may well have other ideas I've not thought of here...
  3 Comments
dpb
dpb on 30 Aug 2022
"...to minimize the amount of time you spend in the toolbox directory you could cd into it, create a function handle to the private function, and return to the previous current directory."
Indeed, Steven, that's an elegant idea that didn't occur to me.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!