How to get uigetfile to give unambiguous full paths of selected files
33 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2023-7-17
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
2023-7-17
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
采纳的回答
Voss
2023-7-17
"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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!