Effacer les filtres
Effacer les filtres

What does addpath do and why should I use it when I am compiling a code in matlab ?

127 vues (au cours des 30 derniers jours)
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\']))

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Mar 2020
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 commentaires
Walter Roberson
Walter Roberson le 14 Mar 2020
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()
farzad
farzad le 17 Mar 2020
Dear Walter ! I really appreciate all your fantastic and elaborative help ! one of the answers I will always remember ! It really really helped !!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by