Get a file path
7 views (last 30 days)
Show older comments
Hello
I'm from Chile and a Matlab noob. I'm triying to work with files and folders. I have to search a folder in the current directory by a part of its name. The part of its name is saved inside a cell array. Then I have to save the folder's path. I don't know if there is a command to do this, I only know uigetdir, and its useless because you have to search the folder on the window.
Regards and apologizes about my english writing
0 Comments
Accepted Answer
Walter Roberson
on 12 Jan 2019
partial_names = {'_MX3', '_NY2'}; %for example
dir_to_search = pwd; %current directory
which_one = randi(length(partial_names));
dinfo = dir( fullfile(dir_to_search, ['*', partial_names{which_one}, '*']);
dinfo( ~[dinfo.isdir] ) = []; %you are only interested in folders
if isempty(dinfo)
disp('No matches')
folder_names = {};
else
folder_names = fullfile( {dinfo.folder}, {dinfo.name} );
end
Now folder_names should be a cell array of fully-qualified folder names whose last component matches the partial name.
Note: in some cases the folder name might be a "canonical name" rather than the name you cd'd to. This can occur if you are using symbolic links or hard links so that there are multiple valid names to reach the same location.
Note: this requires R2016b or later. In earlier versions, getting the canonical name of a folder can be more difficult.
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!