Using addpath with a user defined string
显示 更早的评论
I'm trying to use the addpath command in a script but with a user defined folder name which I want to delete at the end of the script. There are several folders in the path, all containing files of the same name (output from a program) but the folder names are different.
Using foldername=input('Enter the folder name:','s')
then
addpath C:\xxx\'foldername'
Doesn't work because addpath doesn't read the string input. Any thoughts?
1 个评论
Stephen23
2017-1-3
str = fullfile('C:', foldername, 'mydir');
采纳的回答
更多回答(2 个)
Sean de Wolski
2014-7-16
addpath(['C:\xxx\' foldername])
if foldername is a variable
addpath 'C:\xxx\foldername'
if foldername is a string literal
3 个评论
Valkmi
2017-1-3
What if the foldername is a variable and in the middle of the path for example 'C:\foldername\xxx
Image Analyst
2017-1-3
编辑:Image Analyst
2017-1-3
Use sprintf() to build a string
folder = sprintf('C:/%s/blahblah', foldername)
addpath(folder)
Guillaume
2017-1-3
编辑:Image Analyst
2017-1-3
folder = fullfile('C:', foldername, 'blahblah');
Image Analyst
2017-1-4
Why not be friendly to your user and just use uigetdir(), rather than be cruel and make them type it in, and possibly have to go to File Explorer to find the name if they don't know it already?
startingFolder = pwd; % Wherever you want....
folderName= uigetdir('Enter the folder name:', startingFolder)
% Add folder to search path.
addpath(folderName);
path % Print path to command window.
% Later . . . Remove folder from search path.
rmpath(folderName);
path % Print path to command window.
类别
在 帮助中心 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!