Uigetdir to pick multiple directories

I was wondering if anyone knows a simple way how to use uigetdir to select multiple directory paths in a similar way to using uigetfile with 'multiselect','on'
Thanks

回答 (2 件)

Image Analyst
Image Analyst 2011 年 11 月 25 日

0 投票

Not that I know of with uigetdir. However it could work if you searched your hard drive for directories and then listed them all in a very wide listbox where the user could click on multiple directory names right from the listbox. You could use genpath() to load up the listbox.

18 件のコメント

Robbie
Robbie 2011 年 11 月 25 日
Hi, Thanks for your response. I tried playing around with that but I haven't got anything working. Could you maybe explain a little bit more? Thanks
Terry Furqan
Terry Furqan 2020 年 5 月 20 日
i use this to create
root = uigetdir;
sd = genpath(root);
subdir = regexp(sd,';','split');
for k = 2:length(subdir)
F = dir(fullfile(char(subdir(k)), '*.csv')); %some process
end
Image Analyst
Image Analyst 2020 年 5 月 20 日
You don't need to do char(subdir(k)) -- you can simply do subdir{k} to get the contents of the cell. See the FAQ.
startingFolder = pwd;
root = uigetdir(startingFolder);
allSubfolders = genpath(root)
subFolders = regexp(allSubfolders, ';', 'split')
for k = 2 : length(subFolders)
% Get this subfolder.
thisSubFolder = subFolders{k};
% Get a list of CSV files in this subfolder.
theseFiles = dir(fullfile(thisSubFolder, '*.csv'));
fprintf('Found %d CSV files in %s.\n', length(theseFiles), thisSubFolder);
% Some process to use the files.
end
Also, to be clear, this code does not allow the user to specify multiple folders individually. It allows the user to pick a top level folder, and then ALL subfolders under that top folder are examined.
praveen rai
praveen rai 2024 年 8 月 27 日
編集済み: Walter Roberson 2024 年 8 月 29 日
I have similar issue but slightly different
I have one folder in that I have 9 subfolder and in every subfolder one csv file is there
Objective is --> read all csv file from subfolders and store in cell format in one variable so that i can use in other function
I have tried below code
startingFolder = pwd;
root = uigetdir(startingFolder);
allSubfolders = genpath(root)
subFolders = regexp(allSubfolders, ';', 'split')
thisSubFolder=[];
for j = 2 : length(subFolders)
% Get this subfolder.
for i =1:length(subFolders)
thisSubFolder{i} = subFolders{j};
end
% Get a list of csv files in this subfolder.
theseFiles{i} = dir(fullfile(thisSubFolder, '*.csv'));
fprintf('Found %d csv files in %s.\n', length(theseFiles(), thisSubFolder);
% Some process to use the files.
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I wan tto read like this again all csv file
for i=1:length(theseFiles)
fprintf(['\n ***Reading File_',num2str(i),'_of_',num2str(theseFiles)),'***\n']);
Stephen23
Stephen23 2024 年 8 月 27 日
編集済み: Stephen23 2024 年 8 月 27 日
@praveen rai: use DIR better to make your code simpler and more robust, e.g.:
P = uigetdir(); % PWD is the default
S = dir(fullfile(P,'*','*.csv'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = readtable(F); % or whatever function
end
All of the data will be stored in the structure S. For example, the 2nd file:
S(2).folder % location
S(2).name % filename
S(2).data % CSV data
praveen rai
praveen rai 2024 年 8 月 29 日
thanks
praveen rai
praveen rai 2024 年 8 月 29 日
Only thing I noticed that everytime I have to slect csv from each subfolder
is there anyway that I can select parent folder and in that all csv will be selected from all subfolder
I tried this but didnt work --> S = dir(fullfile(P,'*','*.csv','MultiSelect', 'on'));
Stephen23
Stephen23 2024 年 8 月 29 日
編集済み: Stephen23 2024 年 8 月 29 日
"is there anyway that I can select parent folder and in that all csv will be selected from all subfolder"
That is what my code does.
praveen rai
praveen rai 2024 年 8 月 29 日
Hi stepehen with your code showing each csv file and I am selecting manually csv one by one
My aim is click on parent folder --> select all Subfolder --> and atutomatically select csv from each subfolder
Implementation with your code is
Selecting parent folder --> again selecting each csv (popup is coming out everytime to slect csv file)
praveen rai
praveen rai 2024 年 8 月 29 日
Next step what I am trying is
read all csv file and again store into another variable but geeting error
for i=1:length(S(k).data)
fprintf(['\n ***Reading File_',num2str(i),'_of_',num2str(S(k).data)),'***\n']); ---> error in this line
fileData=[];
fileName=[S(k).data];
fileData=csv(fileName); %% here only the first file is stored
Stephen23
Stephen23 2024 年 8 月 29 日
編集済み: Stephen23 2024 年 8 月 29 日
"My aim is click on parent folder --> select all Subfolder --> and atutomatically select csv from each subfolder"
Why do you need to manually "select" the subfolders?
Why do you need to manually "select" the CSV files?
My code lets you select just the the parent folder and it will automatically search all subfolders for all CSV files (no selection of subfolders or CSV files required). It uses READTABLE to import the CSV file content/data into a structure array, which you can access using indexing & fieldnames. Of course you can change READTABLE to something more suitable for your files.
"Implementation with your code is Selecting parent folder --> again selecting each csv"
Nothing in my code requires selecting "each CSV" file (even supports such a functionality).
"popup is coming out everytime to slect csv file"
Nothing in my code will generate a popup (or even supports such a functionality).
I suspect that both of these behaviors are coming from your mystery function CSV.
This code does not make much sense to me:
fprintf(['\n ***Reading File_',num2str(i),'_of_',num2str(S(k).data)),'***\n']); ---> error in this line
What do you expect NUM2STR of the file data to achieve? The FPRINTF format string indicates that you expect it to be a count of the total number of files. That seems very odd and very unlikely that your file contains a count of the total number of files.
fileData=[]; % Why allocate an empty array to fileData?
fileName=[S(k).data]; % Why use superfluous square brackets?
fileData=csv(fileName); %% here only the first file is stored
How exactly should that code should "store" any file ? A "store" is where you can buy food....
What exactly do you mean by "store":
  • allocate data to an array using indexing,
  • export data to a file,
  • something else...?
What is the function CSV? What does it do?
If you want further help please show the complete code you are working on.
Walter Roberson
Walter Roberson 2024 年 8 月 29 日
For "all" subdirectories, perhaps it should be
S = dir(fullfile(P,'**','*.csv'));
instead of
S = dir(fullfile(P,'*','*.csv'));
Stephen23
Stephen23 2024 年 8 月 29 日
編集済み: Stephen23 2024 年 8 月 29 日
"For "all" subdirectories, perhaps it should be""
That would include all subdirectories, subsubdirectories, recursively.
To include only the subdirectories (exactly as specified) one asterisk is correct.
Walter Roberson
Walter Roberson 2024 年 8 月 29 日
"is there anyway that I can select parent folder and in that all csv will be selected from all subfolder"
suggests ** instead of *
Stephen23
Stephen23 2024 年 8 月 29 日
"suggests ** instead of * "
Why? Nothing in the OP's request hints at recursive directories.
Walter Roberson
Walter Roberson 2024 年 8 月 30 日
Saying "from all subfolder" hints more at recursion.
As opposed to if it had been said "all csv will be selected from the subfolders"; that does not imply recursion.
praveen rai
praveen rai 2024 年 9 月 2 日
%%%%%% I am using bsig instead of csv%%%
%%%% bsig can be read using data reader
close all;
clear all;
URL = xxxxx;
P = uigetdir(); % PWD is the default
S = dir(fullfile(P,'*','*.bsig'));
%S = dir(fullfile(P,'*','*.bsig','MultiSelect', 'on'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = uigetfile(F);
end
%% check file data
A =[];
B =[];
for i=1:length(S(k).data)
fprintf(['\n ***Reading File_',num2str(i),'_of_',num2str(S(k).data)),'***\n']);
MatfileData=[];
fileName=[S(k).data];
MatfileData=bsigt(fileName);
RootURL = sprintf('%s.XXXX',URL);
A = MatfileData.readGroup(RootURL);
RootURL = sprintf('%s.YYYY',URL);
B = MatfileData.readGroup(RootURL);
for j= 1:length(e_AlnState)
if ((A(j) == 1 ) || (A(j) == 2))
B(i,:) = rad2deg(a_ElEolMeasHistory(:,j));
median_history(i) = median(B(i,:));
end
end
file_name_temp(i,:)= string(theseFiles(i));
azi_val(i) = extractBetween(file_name_temp(i,:),'_azi=' ,'_eli=' );
azi_angles(i) = str2num( azi_val(i));
% elv_val(i) = extractBetween(file_name_temp(i,:),'_eli=' ,'_nrun' );
figure(1)
h1 = plot(B(i,:) , 'DisplayName', strcat("azi =", azi_val(i)) );
hold on
grid on
ylabel('heading)')
xlabel('history (1:10)')
title('variation')
legend
end
Stephen23
Stephen23 2024 年 9 月 2 日
編集済み: Stephen23 2024 年 9 月 2 日
For some reason you replaced the file data importing function with UIGETFILE:
S(k).data = uigetfile(F);
Why bother using DIR if you are just going to call UIGETFILE on every single file? It also causes exactly the problem that you described here.
Solution: get rid of UIGETFILE.
This line is unlikely to be useful, for reasons that have already been explained here:
for i=1:length(S(k).data)
In my code the DATA field contained the imported file data. Nothing in my code indicates that DATA would contain a filename, so it is unclear where your line of code comes from:
fileName=[S(k).data];
Solution:
  • get rid of things in the code that you do not want (e.g. UIGETFILE on every file, which you added to the code).
  • write one loop, not two.
  • something like this (exactly as I described here):
P = uigetdir(); % PWD is the default
S = dir(fullfile(P,'*','*.bsig')); % or '**' if recursive subfolders
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name); % filename
... code that imports data from file F
S(k).data = ... "store" your data here.
end
Or, if you really want to use two loops then go for it. But do not mix up imported data with filenames.
Tip for writing code: do not just write lots and lots and lots of code without testing what it does, otherwise you will have 30/300/3000/... lines of code full of bugs. And no idea how to fix it or where to start debugging. Making lots of random changes is a bad approach to writing and debugging code.
Write one line. Read the documentation for the functions you use on that one line. Test that one line: does it really do what you expect, does it return the data you expect. If you did this, you would not be mixing up file data with filenames, adding 'MultiSelect' to FULLFILE, etc.

サインインしてコメントする。

Robbie
Robbie 2011 年 11 月 25 日

0 投票

I found a function on the file exchange that seems to work the way i want it 'uipickfiles' but i am open to other ways how to do this. Thanks

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

質問済み:

2011 年 11 月 25 日

編集済み:

2024 年 9 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by