Get a file path

조회 수: 5 (최근 30일)
Loreto RD
Loreto RD 2019년 1월 12일
댓글: Loreto RD 2019년 1월 12일
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

채택된 답변

Walter Roberson
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.
  댓글 수: 1
Loreto RD
Loreto RD 2019년 1월 12일
Thank you so much! I'm going to try that code

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by