Clear Filters
Clear Filters

calling a built-in private function

28 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:
xy = cell(1,N);
parfor k = 1:N
xy{k} = stream3c([],[],[],vx,vy,[],xind(k),yind(k),[],step,maxvert)';
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:
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 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...
Steven Lord
Steven Lord on 30 Aug 2022
The whole purpose behind a private function is that it's only accessible to a limited number of files. Often that means that they may change without warning or without being mentioned in the Release Notes, so be cautious about depending directly on those private functions.
If you want 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.
As an example, the various functions used by gallery to create matrices are located in a private directory in the elmat folder in the MATLAB toolbox directory. One of those functions is minij.m.
previousCurrentDirectory = cd(fullfile(matlabroot, ...
'toolbox', 'matlab', 'elmat', 'private'));
which minij
/MATLAB/toolbox/matlab/elmat/private/minij.m % Private to elmat
fh = @minij;
Because fh was created when the minij function was in scope, it can call the private function even if you're no longer in that directory. But note that calling minij via gallery requires a slightly different calling syntax than calling it directly via the function handle.
which minij % No longer in scope so not found
'minij' not found.
A = gallery('minij', 4)
A = 4×4
1 1 1 1 1 2 2 2 1 2 3 3 1 2 3 4
B = fh(4, 'double')
B = 4×4
1 1 1 1 1 2 2 2 1 2 3 3 1 2 3 4
That being said, if you feel your modifications to stream2 could be of use to other users, please submit an enhancement request to Technical Support. Perhaps in a future release you won't need to call the private functions directly because we improved stream2!
dpb on 30 Aug 2022
" 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)


Find more on Startup and Shutdown in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!