What does addpath do and why should I use it when I am compiling a code in matlab ?
136 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
farzad
el 12 de Mzo. de 2020
Comentada: farzad
el 17 de Mzo. de 2020
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\']))
0 comentarios
Respuesta aceptada
Walter Roberson
el 12 de Mzo. de 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 comentarios
Walter Roberson
el 14 de Mzo. de 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()
Más respuestas (0)
Ver también
Categorías
Más información sobre Search Path en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!