What does addpath do and why should I use it when I am compiling a code in matlab ?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hi All
I am compiliing a code in MATLAB and am advised to use addpaht
what does it do and how does it help me in compiling ?
addpath(genpath([pwd,'\SubFunctions\']))
채택된 답변
Walter Roberson
2020년 3월 12일
genpath() when passed the name of a directory, creates a ":" separated list of that directory and all sub-directories of it that are potentially valid MATLAB paths. That excludes directories whose name starts with @ or + or is the word private (or, in sufficiently new MATLAB releases) or is the word resources
Once the list of directories and subdirectories is created, it is passed to addpath(), which adds the list (by default) to the beginning of the list of directories that is the MATLAB "path".
The MATLAB "path" is the list of directories that is to be searched to find .m and .p and .mex* and .slx and .mld and similar executable files that might be invoked when MATLAB is asked to execute a function. Generally MATLAB starts at the first entry in the list and checks it to see if function matches any executable file in there, then goes on to the second directory in the list, and so on -- so earlier entries in the list generally get priority over later entries. This is not always the case, due to some complications involving classes and packages.
The MATLAB "path" is also the list of directories to be searched to find any data file that is requested to be read in, such as an image file or audio file or .mat file. You can, for example, ask to imshow('cameraman.tif') and MATLAB will search along the path until it finds cameraman.tif to use.
The interactive version of MATLAB adjusts the path at runtime as the code uses cd , But at compile time, MATLAB does not execute the code, so any code that might cd into the SubFunctions directory to be sure to execute the functions there, cannot be taken into account at compile time. The work-around to that is to add to the path all directories that might be cd'd to for that purpose.
And sometimes there are cases when building a large software project where functions that need to be compiled in are not the same as you would use interactively -- for example functions to work around the fact that some functions cannot be compiled.
In short, the addpath is being done to give information to the compiler about which directories to search for functions it might encounter during compilation.
댓글 수: 6
Thank you so much !
Yet , I have my doubts : I have one main .m file plus two other .m files that are functions that will be called by the main files. My intention to create subfolders is that I have several input Excel files and several output Excel files as well, so Input and output should go into separate subfolders and not to make the main folder messy. So , will the addpath help me ? did you mean it works for mfiles management or it's used to find the exe file put in the installation directory of the compiled file where there are other folders ?
and where in my code should I add the addpath ? should it be in the main file and before compilation ?
ok I tried it just on an open Matlab command prompt, it contains many Matlab installation directory folders. So now my question is the same, should this addpath be in my main code? or it's just enough to type it once in command prompt right before compiling ?
It should be at the command line before compiling
You would not typically use addpath to manage input folders. You would never use addpath to manage output folders because matlab will always assume that it should write in the current folder unless you tell it otherwise.
What you would typically do is use relative directories and ctfroot() if the inputs are stored as part of the executable. If the inputs are in user folder you need some way of finding them; uigetdir() or uigetfile() is recommended for that. Once you know which directories you are using you would use fullfile()
Note: when you create a "stand-alone" executable (click on the icon to run it) then the executable has no idea what directory the user is "currently" in. Only "console" applications launched from the dos shell or PowerShell can figure out what the user's "current" directory is.
You can ask the operating system for information about the user's "home" directory or App Data directory and you could assume particular directories relative to that, but doing so does not permit the user to organize their files or use shared directories. It is better to ask the user where they want to use the files.
For this purpose, addpath is only used to control where to find the m files (and related files) to build the executable, not where to look for data files. Data file locations in executables should be managed using fullfile()
Dear Walter ! I really appreciate all your fantastic and elaborative help ! one of the answers I will always remember ! It really really helped !!!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Search Path에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
