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

Rather than concatenating strings, use fullfile:
str = fullfile('C:', foldername, 'mydir');

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2014년 7월 16일

2 개 추천

The single quotes are messed up. Try just passing in the variable (if it's the full path):
addpath(foldername);
Otherwise
folder = sprintf('C:\xxx\%s', foldername);
addpath(folder);
or simply call uigetdir() instead of input , which is probably what you should do.

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2014년 7월 16일

0 개 추천

addpath(['C:\xxx\' foldername])
if foldername is a variable
addpath 'C:\xxx\foldername'
if foldername is a string literal

댓글 수: 3

Valkmi
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
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
Guillaume 2017년 1월 3일
편집: Image Analyst 2017년 1월 3일
Even better, use fullfile to build file paths. It will ensure that the correct separator is used:
folder = fullfile('C:', foldername, 'blahblah');

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2017년 1월 4일

0 개 추천

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에 대해 자세히 알아보기

질문:

2014년 7월 16일

댓글:

2020년 10월 4일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by