MATLAB: Get a file path

folderget pathpath

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

Best Answer

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.