필터 지우기
필터 지우기

How to access all .m files in different subfolders?

조회 수: 23 (최근 30일)
Zoltán Csáti
Zoltán Csáti 2014년 7월 7일
답변: benjamin rinauto 2016년 10월 18일
As my project grows, I came up with the idea to arrange the different functions into different folders. For example the solvers are in the "solver" folder, the utility functions in the "utilities" folder, etc. If I invoke my functions, the do not see each other because of being different folders. How can I eliminate this error? I would like to maintain this several-folders structure. Do I have to change the current directory all the time?
Thanks, Zoli

채택된 답변

Star Strider
Star Strider 2014년 7월 7일
편집: Star Strider 2014년 7월 7일
As part of my startup.m file, I start by calling the userpath function, then genpath and lastly, addpath. I’m not certain this is the most efficient method, since my startup.m file has evolved over the 15 or so years since MATLAB added the MATLAB directory to Documents, but it works.
Specifically:
MyPath = userpath;
MyDir = MyPath(1:strfind(MyPath,';')-1);
MyWorkDir = genpath(MyDir);
addpath(MyWorkDir, '-end');
EDIT — This works for Windows. No promises for Mac or Linux systems.
  댓글 수: 2
Zoltán Csáti
Zoltán Csáti 2014년 7월 7일
Thank you very much, it works on Windows.
Star Strider
Star Strider 2014년 7월 7일
My pleasure!

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

추가 답변 (2개)

Aldo Amaya
Aldo Amaya 2014년 7월 7일
If you are only calling functions you do not have to change to a current directory. You just need to set the path to include all folders.
Environment>Set Path>Add With Subfolders> (Select work folder)
Then you should be able to call upon any function in your path.
  댓글 수: 5
Zoltán Csáti
Zoltán Csáti 2014년 7월 7일
I solved it with your (interactive) method and with Star Strider's startup version. Both of them works. Thank you too.
Zoli
Aldo Amaya
Aldo Amaya 2014년 7월 7일
Sounds good.

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


benjamin rinauto
benjamin rinauto 2016년 10월 18일
It sounds like this file on the file exchange is what you're looking for:
https://www.mathworks.com/matlabcentral/fileexchange/59815-addcontainingdirandsubdir--

카테고리

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