MATLAB: To Create a Variable with the name of a folder

variable

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!

Best Answer

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: