Populate a uitable with files of directory

1 vue (au cours des 30 derniers jours)
Gustavo
Gustavo le 30 Août 2014
Commenté : Image Analyst le 30 Août 2014
Can anyone help me populate a uitable with the path and file names from a directory?
Note: with "display (path)" is correctly printing all files is caught on.
or
% Populate a uitable with files of directory
nameDir = uigetdir();
listDir = strcat(nameDir, '/*.jpg');
files = dir(listDir);
size_ = length(files);
for l = 1: size_
nameImg = files(l, 1).name;
path = strcat(nameDir, '/', nameImg);
% display(path);
set(handles.uitable1, 'Data', {path});
end

Réponses (1)

Geoff Hayes
Geoff Hayes le 30 Août 2014
Gustavo - you just about have the correct code. All that is missing is to build the cell array of files names within the for loop, and then set the data of the uitable outside of the loop. Something like
nameDir = uigetdir();
% guard agains the user cancelling the uigetdir dialog
if ischar(nameDir)
listDir = strcat(nameDir, '/*.jpg');
files = dir(listDir);
numFiles = length(files);
% set the size of the cell array
paths = cell(numFiles,1);
% build the cell array of files
for l = 1:numFiles
nameImg = files(l,1).name;
paths{l} = fullfile(nameDir, nameImg);
end
set(handles.uitable1, 'Data', paths);
end
The above is very similar to what you have. The code just guards against the user cancelling the get directory dialog (returns 0 on pressing of the cancel button). It also makes use of fullfile to build the file name from the parts (the folder and file name) - that way, you don't have to be concerned with the forward or backward slash. Once the cell array is built, then the uitable is seer with the path data.
Try the above and see what happens!
  2 commentaires
Image Analyst
Image Analyst le 30 Août 2014
Modifié(e) : Image Analyst le 30 Août 2014
I probably wouldn't use l (lower case L) as the name of a variable because it looks too much like 1 (one) and I (upper case i), but otherwise this is right on.
Image Analyst
Image Analyst le 30 Août 2014
Gustavo's "Answer" moved here since it's not an answer to the original question, and so he can "Accept" Geoff's answer instead of his own:
Thank you for your help, your fix of code worked as I expected!
Again thank you by help!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by