Matlab:如何动态使用javaaddpath

Matlab: How to use javaaddpath dynamically

我想在我的 linux 和 window 计算机上使用具有不同路径的 javaaddpath。

但是,我希望它是真正的动态分配。也就是说,用户可以自己定义his/her Path_Str = ' ....../ParforProgMonv2/java' 然后在这一步传递:pctRunOnAll javaaddpath (Path_Str)

打开matlab pool后,我想做这样的事情:

if strcmp(MonitorProcess, 'Yes')
    %add this line for progress monitor
    pctRunOnAll javaaddpath ('/home/dkumar/ParforProgMonv2/java')
end

但是,我想包含在

之间选择的动态路径,而不是固定路径 '/home/dkumar/ParforProgMonv2/java'

'/home/dkumar/ParforProgMonv2/java''C:/Users/DK_GS/ParforProgMonv2/java'

取决于它是我的 window 电脑还是 linux。

我尝试使用 ClassPathHacker.java 关注 this solution;然而,没看懂。

一些帮助将不胜感激。

这样的东西行得通吗?

searchpath = 'ParforProgMonv2/java'; % Directory to search for

if strcmp(MonitorProcess, 'Yes')
    switch computer
        case {'PCWIN', 'PCWIN64'}
            % 32 or 64 bit Windows
            % Use the system command to return all directories found on the machine
            % that match your search directory. Use a regex to clean up the list
            [~, cmdout] = system(['dir /s/b/AD | find "' searchstr '"');
            allpaths = regexp(cmdout, '(.:\[\w\-\. ]+\w+(?=\s))', 'match'); % Split directory names, 1st cell should be the top level
            pctRunOnAll javaaddpath (allpaths{1})
        case 'GLNXA64'
            % Linux
            pctRunOnAll javaaddpath ('/home/dkumar/ParforProgMonv2/java')
        otherwise
            % Insert error handling here
    end
end

其中 computer return 是一个字符串,指定当前 运行 的计算机类型。

编辑:根据您的评论,我建议添加一种方法来搜索您的文件路径和 return 一个字符串。我为 Windows 添加了一个示例;我对 Linux 不够熟悉,无法正确翻译。