Get a file path

7 visualizzazioni (ultimi 30 giorni)
Loreto RD
Loreto RD il 12 Gen 2019
Commentato: Loreto RD il 12 Gen 2019
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

Risposta accettata

Walter Roberson
Walter Roberson il 12 Gen 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.
  1 Commento
Loreto RD
Loreto RD il 12 Gen 2019
Thank you so much! I'm going to try that code

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by