Get a file path
조회 수: 5 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2019년 1월 12일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!