To Create a Variable with the name of a folder

조회 수: 12 (최근 30일)
Peter
Peter 2015년 1월 2일
댓글: Stephen23 2015년 1월 2일
Hello!
I have a variable called this way;
workfolder='C:\Users\Jorge\Desktop\Work_Christmas\Measurements\rpm_measurements\rpm0800\';
And i would like to create another variable and it is the end of the name of my directory (workfolder), in order to use it with a loop, in this case it would be:
new_variable = rpm0800
How could i do it?
Thanks!
  댓글 수: 2
Jan
Jan 2015년 1월 2일
Do you mean:
new_variable = 'rpm0800'
with the quotes?
Peter
Peter 2015년 1월 2일
Yes exactly!

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

채택된 답변

Stephen23
Stephen23 2015년 1월 2일
편집: Stephen23 2015년 1월 2일
While the question is rather unclear, it seems that the OP wishes to extract the last directory in the filepath string. This can be achieved using fileparts :
>> [A,B] = fileparts(workfolder(1:end-(workfolder(end)==filesep)))
A = 'C:\Users\Jorge\Desktop\Work_Christmas\Measurements\rpm_measurements'
B = 'rpm0800'
Note first this removes any trailing file separator character using the indexing of the workfolder string. An alternative is to use regexp to locate the last directory in the filepath string:
>> regexp(workfolder,'[^/\\]+(?=[/\\]?$)','once','match')
ans = 'rpm0800'
You should also have a good read of the MATLAB wiki, which covers all of these basic usage questions, with handy examples too:
  댓글 수: 2
Peter
Peter 2015년 1월 2일
Thank you very much Stephen, it worked
Stephen23
Stephen23 2015년 1월 2일
My pleasure!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 1월 2일
It's usually advised that you don't do that . However there is capability in MATLAB to do it and the FAQ shows some ways, such as using dynamic structure field names.
  댓글 수: 4
Image Analyst
Image Analyst 2015년 1월 2일
If, like I do, you have trouble figuring out complicated regexp expressions like '[^/\\]+(?=[/\\]?$)' , or would take too much time figuring that out, then you can use John D'Errico's incredibly handy and easy-to-use utility called allwords http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords. It splits up the string into separate words and gives you control over the word separators. Here is some code that I think you'll find a lot easier to use and remember to use than those cryptic regexp expressions:
workfolder='C:\Users\Jorge\Desktop\Work_Christmas\Measurements\rpm_measurements\rpm0800\';
subFolders = allwords(workfolder, '\')
deepestFolder = subFolders{end}
I left off the semicolons at the end of the lines so you can see what it produces in the command window:
subFolders =
'C:' 'Users' 'Jorge' 'Desktop' 'Work_Christmas' 'Measurements' 'rpm_measurements' 'rpm0800'
deepestFolder =
rpm0800
What do you think, Peter? Is allwords() easier/simpler? Which do you prefer?
Stephen23
Stephen23 2015년 1월 2일
It is true that Regular Expressions have a rather cryptic syntax and can take a while to master, however they are a powerful tool that can be met in many different programming languages, and thus they are well worth the effort of learning. The fact that regexp is inbuilt means your code remains portable, without relying on any third-party functions (and licences...)
The example that Image Analyst gives above could also be achieved using standard MATLAB regexp:
>> regexp(workfolder,'\w+','match')
ans = {'C','Users','Jorge','Desktop','Work_Christmas','Measurements','rpm_measurements','rpm0800'}
Users wanting to practice and learn about Regular Expressions can try my compact Regular Expression Helper, which opens a figure and uses real-time updating of regexp's inputs and outputs to allow endless experimentation and refining of Regular Expressions:

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

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by