call functions from subpath

170 ビュー (過去 30 日間)
Matthias Pospiech
Matthias Pospiech 2012 年 3 月 4 日
コメント済み: SGUNITN 2018 年 2 月 14 日
I have a function called latexfigure which is based on several functions which are placed in subpaths.
The directory structure is thus the following
/libs/latexfigure/latexfigure.m
/libs/latexfigure/exportfig/fix_lines.m
/libs/latexfigure/strsplit/strsplit.m
/libs/latexfigure/../*.m
and an example file using it would add the lib to the path to use it:
addpath('/libs/latexfigure/');
hfig = figure(1); clf;
plot(...)
latexfigure(hfig,'latexfigure','png');
However this fails because latexfigure needs to have all its subdirs in the path.
So how can I achieve this? Can I find out the path of the script itself and add all subdirs? Or can I run functions with path?
  1 件のコメント
SGUNITN
SGUNITN 2018 年 2 月 14 日
Please try using . in the path. And add the other paths as well. However, adding the paths globally is other option (HOME -> SET PATH)
addpath('./libs/latexfigure/');

サインインしてコメントする。

回答 (5 件)

Daniel Shub
Daniel Shub 2012 年 3 月 4 日
I am posting this as a new answer because I think it is long enough, but it is based on a comment to my previous answer.
If you look at how MATLAB handles the paths of its toolboxes, you will see that all directories and subdirectories get added to the path. To be consistent with the MATLAB way of installing a toolbox you should add all the paths. This can be done with:
addpath(genpath('/libs/latexfigure/'));
If you don't want this there are 4 ways to proceed.
  1. You could merge all your directories into one big directory (ugly, but it gets the job done).
  2. You could merge all your subdirectories into a directory called "private" (case sensitive) which would not need to be added to the path. Functions in "private" directories are only accessible from functions in the immediate parent directory. A "private" directory cannot have subdirectories.
  3. Your functions could carefully manage the path. Each function could query its location, use genpath and path to determine the missing elements from the path, add the elements, and then remove them at the end. You would want to add an onCleanup catch to handle crashes. This is a lot of work and potentially problematic if you really care about maintaining the path. If not, you can just have each function add all the subdirectories.
  4. You could create a package. This is the most work, but it might be what you want. I find MATLAB packages frustrating to use and work with.
  2 件のコメント
Matthias Pospiech
Matthias Pospiech 2012 年 3 月 4 日
I have chosen number 3, but the removal is tricky because I leave the function using 'return' on errors, but then I do not execute any rmpath statement.
Daniel Shub
Daniel Shub 2012 年 3 月 4 日
The onCleanup object will take care of exiting based on return.

サインインしてコメントする。


Jerry Wolfensberger
Jerry Wolfensberger 2016 年 5 月 10 日
To add functions in subfolders, you can use relative paths.
Firstly you will need all subfolders (if you dont want to hardcode them). For that you can use the dir and isdir function.
currentFolderContents = dir(pwd); %Returns all files and folders in the current folder
currentFolderContents (~[currentFolderContents.isdir]) = []; %Only keep the folders
Now the currentFolderContents structure contains all subfolders of your current folder as well as the paths to the current and topfolder ('.' and '..' (These should always be the first two entries)).
To add the subfolders to your path use a for loop
for i = 3:length(currentFolderContents) %Start with 3 to avoid '.' and '..'
addpath(['./' currentFolderContents(i).name])
end
For nested subfolders use nested for loops.
Hope that helps.
Cheers

Daniel Shub
Daniel Shub 2012 年 3 月 4 日
You may want to look at
doc genpath
For example
addpath(genpath('/libs/latexfigure/'));
might fix your problems.
EDIT You can do either ...
function addallsubpathforscript(name)
addpath(genpath(fileparts(which(name))));
or
function latexfigure(...)
addpath(genpath(fileparts(mfilename('fullpath'))));
  2 件のコメント
Matthias Pospiech
Matthias Pospiech 2012 年 3 月 4 日
This is not possible, because the latexfigure itself must be able to fix the problem. Your code would require that the user who uses my lib function fixes the problem.
I require something as
addallsubpathforscript('latexfigure.m');
or
function latexfigure(...)
addallsubpathofcurrentfunction;
Daniel Shub
Daniel Shub 2012 年 3 月 4 日
See my edit ...

サインインしてコメントする。


Matthias Pospiech
Matthias Pospiech 2012 年 3 月 4 日
I solved it now with 'mfilename'
function latexfigure(h,FileName,outputFormat,varargin)
currmfile = mfilename('fullpath');
currPath = currmfile(1:end-length(mfilename()));
addpath([currPath 'exportfig']);
addpath([currPath 'gsconvert']);
addpath([currPath 'matlabfrag']);
addpath([currPath 'strsplit']);
  1 件のコメント
Daniel Shub
Daniel Shub 2012 年 3 月 4 日
That is basically my edit. This corrupts the user path and can cause problems if there are name conflicts. You also should look at fileparts:
currPath = fileparts(mfilename('fullpath'));

サインインしてコメントする。


benjamin rinauto
benjamin rinauto 2016 年 10 月 18 日
Try this file on the file exchange. It sounds like what you need:
https://www.mathworks.com/matlabcentral/fileexchange/59815-addcontainingdirandsubdir--

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by