在MATLAB脚本中自动添加路径 [英] automatically add path in a MATLAB script

查看:812
本文介绍了在MATLAB脚本中自动添加路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个MATLAB脚本可以与同事共享.我已将这些脚本放在指定的目录下,例如/home/sharefiles

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

在MATLAB命令提示符下,用户可以通过键入

Under the MATLAB command prompt, the users can use these scripts by typing

addpath  /home/sharefiles

有没有一种方法可以自动在我的matlab脚本中添加此路径,并节省用户每次调用addpath /home/sharefiles的工作量.

Is there a way to automatically add this path in my matlab script, and save users the efforts of invoking addpath /home/sharefiles each time.

推荐答案

当然,只需将addpath添加到脚本中即可.

Sure, just add the addpath to your script.

addpath('/home/sharefiles')

如果要递归添加子目录,请使用genpath函数:

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

addpath(genpath('/home/sharefiles')

将文件添加到路径或Matlab中较慢的操作之一,因此您可能不想将addpath调用放在操作的内部循环中.您还可以进行测试,看看是否需要首先添加路径.

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

或者,更直接地

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

这篇关于在MATLAB脚本中自动添加路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆