Make one word in path name a variable

조회 수: 6 (최근 30일)
Nancy Hammond
Nancy Hammond 2019년 8월 26일
댓글: Image Analyst 2019년 8월 26일
Can I make one variable in a path name a variable?
'C:\Users\Jane....'
'C:\Users\George....'

답변 (2개)

the cyclist
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);

Image Analyst
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
Nancy Hammond
Nancy Hammond 2019년 8월 26일
It's the name after Users that I want to be variable. The resto if the path nake and file name remain the same
This was the Matlab code I was using. But with multi files, this adds too much complexity to the code:
if home ==1
xfb= load ('C:\Users\Nancy\Documents\Research\Data\CRSP\famablissprices.txt');
else
xfb= load ('C:\Users\Zenobia\Documents\Research\Data\CRSP\famablissprices.txt');
end
So would that be:
varName = pathNameVar(slashIndices(2):slashIndices(3));
Thank you for showing me how to think about this.
Image Analyst
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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by