MATLAB:将当前文件夹设置为脚本的位置 [英] MATLAB: Set current folder to script's locaton

查看:643
本文介绍了MATLAB:将当前文件夹设置为脚本的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的文件夹中有一些脚本和数据,并且我经常使用addpath和相对路径.我的问题是,仅当当前文件夹位于我执行的脚本所在的位置时,此方法才有效.例如,如果我执行添加路径X的脚本A,然后执行路径X中的脚本B,则Matlab不会自动更改文件夹,并且脚本B中指定的相对路径不再起作用.

I have a handful of scripts and data in different folders and I use addpath and relative paths very often. My problem is, this only works if my current folder is where the script that I execute is located. For example, if I execute script A which adds path X and later execute script B which lies in path X, Matlab doesn't automatically change the folder and relative paths specified in script B don't work anymore.

有没有一种方法可以将当前文件夹自动设置为我正在执行的脚本的位置?

Is there a way to automatically set my current folder to the location of the script I'm executing?

/edit:我应该注意,我在具有不同驱动器名称的不同计算机上使用了这些脚本,因此使用绝对路径可能无济于事.

/edit: I should note that I use these scripts on different computers with different drive names, so using absolute paths probably won't help.

推荐答案

在脚本中添加以下行,它将设置当前目录=脚本目录

Put the following line in the script, it would set the current directory = script directory

cd(fileparts(mfilename('fullpath')))

这篇关于MATLAB:将当前文件夹设置为脚本的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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