calling a built-in private function

조회 수: 15 (최근 30일)
Chad Greene
Chad Greene 2022년 8월 29일
댓글: dpb 2022년 8월 30일
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?

채택된 답변

dpb
dpb 2022년 8월 29일
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
Steven Lord
Steven Lord 2022년 8월 30일
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.
cd(previousCurrentDirectory)
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
dpb 2022년 8월 30일
"...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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by