Make one word in path name a variable
41 次查看(过去 30 天)
显示 更早的评论
Can I make one variable in a path name a variable?
'C:\Users\Jane....'
'C:\Users\George....'
0 个评论
回答(2 个)
the cyclist
2019-8-26
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);
0 个评论
Image Analyst
2019-8-26
With "names" as your existing variable, try fullfile() to create a new variable with name "thisFolderName":
names = {'Jane', 'George', 'Nancy', 'Image Analyst'};
'C:\Users\Jane....';
for k = 1 : length(names)
thisFolderName = fullfile('C:\users\', names{k})
% Now do something with thisFolderName
end
You'll see
thisFolderName =
'C:\users\Jane'
thisFolderName =
'C:\users\George'
thisFolderName =
'C:\users\Nancy'
thisFolderName =
'C:\users\Image Analyst'
2 个评论
Image Analyst
2019-8-26
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!