automatically add path in a MATLAB script

前端 未结 3 997
生来不讨喜
生来不讨喜 2021-01-02 11:01

I have several MATLAB scripts to share with my colleagues. I have put these scripts under a specified directory, e.g., /home/sharefiles

Under the MATLAB command prom

3条回答
  •  抹茶落季
    2021-01-02 11:30

    Sure, just add the addpath to your script.

    addpath('/home/sharefiles')
    

    If you want to recursively add subdirectories, use the genpath function:

    addpath(genpath('/home/sharefiles')
    

    Adding files to the path or one of the slower operations in Matlab, so you probably don't want to put the addpath call in the inner loop of an operation. You can also test to see if you need to add the path first.

    if ~exist('some_file_from_your_tools.m','file')
        addpath('/home/sharefiles')
    end
    

    Or, more directly

    if isempty(strfind(path,'/home/sharefiles;'))
        addpath('/home/sharefiles')
    end    
    

提交回复
热议问题