如何在 Matlab 中以可移植的方式获取路径目录列表?

How to get path directories list in portable way in Matlab?

path function returns 作为长连接字符串的路径,由平台相关的分隔符分隔

是否可以通过便携方式获取路径中的目录列表?


目前我在写:

function [ res ] = pathdirs(  )
%PATHDIRS Returns all path dirs as a cell array of strings
    p = path;
    if ispc
        sep = ';';
    else
        sep = ':';
    end
    res = strsplit(p, sep);
end

可以做得更好吗?

如何处理路径以使其可移植:

myPath = path;
myPortablePath = strsplit(myPath, ';');

使用上面的代码,myPortablePath 是一个元胞数组,每个元胞包含单独的目录。

依赖于平台的分隔符是pathsep

function res = pathdirs
%PATHDIRS Returns all path dirs as a cell array of strings
    res = strsplit(path, pathsep);
end