Make one word in path name a variable

39 views (last 30 days)
Nancy Hammond
Nancy Hammond on 26 Aug 2019
Commented: Image Analyst on 26 Aug 2019
Can I make one variable in a path name a variable?

Answers (2)

the cyclist
the cyclist on 26 Aug 2019
Yes, but you will need to be a little more specific about the rule for finding what part of the string you want. One step that might help you is to use the regexp command to find the locations of the slashes:
pathNameVar = 'C:\Users\Jane....';
slashIndices = regexp(pathNameVar,'\');
So, for example, if you wanted everything after the last slash to be your variable, then
varName = pathNameVar(slashIndices(end)+1:end);

Image Analyst
Image Analyst on 26 Aug 2019
With "names" as your existing variable, try fullfile() to create a new variable with name "thisFolderName":
names = {'Jane', 'George', 'Nancy', 'Image Analyst'};
for k = 1 : length(names)
thisFolderName = fullfile('C:\users\', names{k})
% Now do something with thisFolderName
You'll see
thisFolderName =
thisFolderName =
thisFolderName =
thisFolderName =
'C:\users\Image Analyst'
Image Analyst
Image Analyst on 26 Aug 2019
How is the program supposed to know where the names Nancy and Zenobia come from? Are they already folders on the disk, like you can just use dir('c:/users/') to find out about?

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!