Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 这个setup.m文件是做什么用的?_Matlab - Fatal编程技术网

Matlab 这个setup.m文件是做什么用的?

Matlab 这个setup.m文件是做什么用的?,matlab,Matlab,setup.m文件做什么 function setup %SETUP Adds directories for Metrics to your MATLAB path % % Author: Ben Hamner (ben@benhamner.com) myDir = fileparts(mfilename('fullpath')); paths = genpath(myDir); paths = strread(paths,'%s','delimiter',':'); paths

setup.m文件做什么

    function setup
%SETUP Adds directories for Metrics to your MATLAB path
%
%   Author: Ben Hamner (ben@benhamner.com)

myDir = fileparts(mfilename('fullpath'));
paths = genpath(myDir);
paths = strread(paths,'%s','delimiter',':');
pathsToAdd = [];

for i=1:length(paths)
    thisPath = paths{i};
    thisPathSplit = strread(thisPath,'%s','delimiter','/');
    addThisPath = 1;

    % Do not add any directories or files starting with a . or a ~
    for j=1:length(thisPathSplit)
        thisStr = thisPathSplit{j};
        if (~isempty(thisStr)) && ((thisStr(1) == '.') || (thisStr(1) == '~'))
            addThisPath = 0;
        end
    end
    if addThisPath ==1
        if ~isempty(pathsToAdd)
            thisPath = [':' thisPath];
        end
        pathsToAdd = [pathsToAdd thisPath];
    end
end

addpath(pathsToAdd);
savepath;

我从描述中了解到,它将目录添加到Matlab的搜索路径中。但是哪一个,为什么?我的Matlab脚本经常散落在
addpath('data')
行中。这是否意味着我不必再这样做了?非常感谢您的评论

这是添加setup.m所在的目录以及该目录中的每个子目录
fileparts(mfilename('fullpath'))
获取文件所在的目录,以及
genpath(myDir)获取所有子目录。请注意,这将删除以“.”或“~”开头的任何目录。

您正在链接的文件是度量包的设置文件-它将路径添加到各个文件夹中,以便您可以使用度量包而无需手动设置路径。
更具体地说,
setup.m
函数在其所在级别及其以下添加所有路径。如果您将此文件复制到任何目录并运行它-它将添加此目录及其所有子目录和子目录的子目录等(不包括以
~
开头的文件夹)

但我有预感,你要找的是:

小更正-它不添加当前目录(及其子目录),而是添加
setup.m
所在的目录(及其子目录)。是的,你是对的,我刚刚更新了答案。谢谢。为了回答您的相关问题-您是否需要在整个脚本中分散调用
addpath
。您可以使用一个单独的脚本/函数来添加所需的所有路径,这样更容易。我通常有单独的“项目”脚本(例如
proj_ADHD
),它们只用于设置特定项目的工作环境-它们添加路径,有时还加载文件<如果需要添加子文件夹的层次结构,code>genpath
非常有用。顺便说一句:这里有一个函数可以(几乎)实现相同但更易于理解:唯一的区别在于不添加以
~
开头的文件夹-链接函数仅在顶层(在其所在的文件夹中)检查并排除以
开头的路径。