Is there a drawback for calling MATLAB addpath multiple times?

My questions: if addpath is like #include in C. In C, if you don't add #include guard (#ifndef ...), there will be many function definitions. But it seems MATLAB is dealing with this problem.

I used this circuit to call addpath multiple times:

try f(sample args); catch err addpath('lib'); end 

But now I think it is not necessary.

+4
source share
2 answers

#include adds a special header file . addpath simply adds the folder to the search path and does not add code to your program. Think about this by adding directories to search for header files in C ++ (for example, in Visual Studio, in "Additional inclusion directories" and g ++ using -I ).

Also, I think addpath checks to see if a folder has been added, so you really do nothing with repeated calls to addpath('lib') .

+5
source

Several calls to addpath do not create several functions, so from the point of view of correctness there are no problems using addpath several times.

However, addpath is a relatively slow operation. You should not call it a function that can be called many times during normal operation.


Edit:

Alternatively, instead of relying on try / catch to check the current state of your path, you can directly check the path. See examples here: fooobar.com/questions/663966 / ....

+3
source

Source: https://habr.com/ru/post/1395810/


All Articles