how to use functions from private directories

조회 수: 22 (최근 30일)
Noam Greenboim
Noam Greenboim 2015년 6월 7일
댓글: Jan 2015년 6월 7일
While I was trying to improve the performance of Excel file reading and writing, I've noticed that xlsread is using some other functions, located in a directory called ...\toolbox\matlab\iofun\private. I wrote a new function, which is composed of the code of xlsread, but it was impossible to access some functions that reside in the location above.
I tried using addpath:
addpath(fullfile(matlabroot, 'toolbox\matlab\iofun\private\'))
but I got a warning saying:
Warning: Private directories not allowed in MATLAB path
1. what's the reason for that?
2. are there any workarounds except of copying all the necessary related functions to my working directory?

채택된 답변

Jan
Jan 2015년 6월 7일
You can have different private functions with the same name in different folders. If you inclucde such a private folder in the path, it is visible from all other functions also and then they are not "private" anymore. This can lead to ambigous dependencies and therefore it is nopt allowed.
The clean solution is to create a private folder in the folder, which contains your function, and copy the required files. Another, less clean method, is to add a wrapper function in the toolbox folder:
function varargout = myPrivateWrapperXYZ(FuncName, varargin)
[varargout(1:nargout)] = FuncName(varargin{:});
Care for a meaningful function name. This forwards all requests even to the private functions. The drawback is, that adding functions to Matlab's toolbox paths requires admin privileges and it is not portable. After a re-installation of Matlab your code will fail, and this is a bad idea in general.
  댓글 수: 2
Noam Greenboim
Noam Greenboim 2015년 6월 7일
Thanks. I've found some information here .
I didn't want to make copies of all the necessary files, so I placed all the relevant functions as local functions within one code file.
It seems to work OK.
Another quick-and-dirty solution I was thinking of is to change directory to the private directory, and switch back at the end.
Jan
Jan 2015년 6월 7일
Setting the current directory to a folder of Matlab's toolbox folders can have strange side-effects. E.g. all Matlab functions, which are called implicitly will see the private functions also. But these functions are located in a private folder with the purpose, that they are not visible from the outside.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 6월 7일
편집: Walter Roberson 2015년 6월 7일
Private functions cannot be called directly from outside routines, except by making copies of them.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by