How to get uigetfile to give unambiguous full paths of selected files

25 vues (au cours des 30 derniers jours)
Andrew Morris
Andrew Morris le 17 Juil 2023
Commenté : Voss le 17 Juil 2023
Hi, I'm currently having an issue where it is difficult to load multiple files from the uigetfile() function.
I have a function which we will call myfun which takes as an argument a string containing the filename to be opened, or the full path to the filename if it is not in the working directory. It can only accept a single string this way. I did not write this function, and treat it as a black box, so I cannot easily edit it to change how the input works. What I would like to do is use uigetfile to select multiple files, potentially from different paths, and then pass the strings contained in the cells to myfun as a full path for it to open in a for loop all files this way and collect the output from myfun. In pseudocode it would look like:
[filenames paths] = uigetfile("*.filetype", "title", "MultiSelect", "on");
for i=1:numel(filenames)
fullpath = strcat(paths{i},filenames{i});
output(i) = myfun(fullpath);
end
the problem is, that if multiple files are selected in the same path, only one cell is added to the paths array. If I have 10 files selected, and 2 of them are in the same directory, I will have 9 cells in paths and 10 cells in filenames, and no a piori way to know which two files share a common path.
To make things more confounding, If only one file is selected or all files are from the same directory then either filenames or paths respectively will not be cell arrays but uigetfile outputs a string instead. Why this function doesn't just produce a 1x1 cell array, so that it can be handled in the same way regardless of the size of the output seems like an oversight, but it's easy to test whether the output is cell or string. The biggest problem is the inability to unambiguously produce full paths regardless of whether the files came from the same directory.
Do I need to use a different function to do this, or else is there an elegant way to produce aa list of full paths to the files selected?
EDIT: It seems like the issue is that uigetfile cannot select mutliple files if they are in different paths. So I guess my question becomes is there a way to do this other than moving all input files into the same directory?
  2 commentaires
Walter Roberson
Walter Roberson le 17 Juil 2023
It seems like the issue is that uigetfile cannot select mutliple files if they are in different paths
That is correct. uigetfile() can only return a single directory, not one directory for each file.
Walter Roberson
Walter Roberson le 17 Juil 2023
Recommended improvement:
[filenames, filepath] = uigetfile("*.filetype", "title", "MultiSelect", "on");
if isnumeric(filenames); return; end %user cancelled
filenames = cellstr(filenames); %if user selected only one, result is a char vector
filenames = fullfile(filepath, filenames);
numfile = numel(filenames);
output = zeros(numfile, 1);
for K = 1:numfile
fullpath = filenames{K};
output(K) = myfun(fullpath);
end

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 17 Juil 2023
"uigetfile cannot select mutliple files if they are in different paths"
One way around this limitation would be to make multiple calls to uigetfile, getting whatever files you need wherever they are. Click cancel in (or close without selecting any files) the file selection dialog when finished. Then proceed to run myfun on each file selected.
filenames = {};
while true
[new_filenames,new_path] = uigetfile("*.filetype", "title", "MultiSelect", "on");
if isnumeric(new_filenames)
% user cancelled -> done selecting files
break
end
if ~iscell(new_filenames)
new_filenames = {new_filenames};
end
filenames = [filenames fullfile(new_path,new_filenames)];
end
for i=1:numel(filenames)
output(i) = myfun(filenames{i});
end
  2 commentaires
Andrew Morris
Andrew Morris le 17 Juil 2023
Thank you! that works quite well!
Voss
Voss le 17 Juil 2023
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by