User input file directory
28 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bob Thompson
am 26 Jul. 2016
Kommentiert: Bob Thompson
am 1 Aug. 2016
I am writing a script that reads all of the files of a certain type in a given directory, and I'm wondering if there is a way to allow the directory location to be a user input.
Currently the code looks like:
directory_in = dir('C:\Folder\Bob\*.dat');
I would like to change it to something more like:
input_in = input('Enter the directory name: ');
directory_in = dir(input_in, '*.dat');
I know this code won't work as written for multiple reasons, but I'm wondering if there is some way I could combine the user input with the file type suffix in order to minimize the amount of editing a future user would need to perform on the script.
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 27 Jul. 2016
You might try uipickfiles(). http://blogs.mathworks.com/pick/2010/02/19/file-and-folder-selection-gui/
I'm not a big fan of uigetdir(). It would be better if you could see the files, but had them disabled. I hear all the time from my users that they're confused because they can't see the files in the folders so they aren't sure if they're in the right folder. Of course you could use uigetfile() instead, but the problem with doing that is that it requires the user to pick a file. Not good when you're wanting them to select a folder and not a file. I wish uigetdir() had an option to show files but disabled the user from actually selecting any of them.
2 Kommentare
Image Analyst
am 28 Jul. 2016
I'm not sure I understand why you can't get only .data files like explained in the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
startingFolder = pwd; % Whatever.
folder = uigetdir(startingFolder);
% Now get .dat files
filePattern = fullfile(folder, '*.dat');
files = dir(filePattern);
for k = 1 : length(files)
fullFileName = fullfile(folder, files(k).name);
fprintf('Now processing file %s...\n', fullFileName);
end
Tell me why the above code makes you say "If I use file_in = uigetdir and then dir(file_in) then I am unable to call only .dat files." Is it because you passed the folder in to dir() instead of a file pattern? Well maybe that's a case to use more descriptive variable names like I did. Is it some other reason?
Weitere Antworten (2)
Bob Thompson
am 27 Jul. 2016
4 Kommentare
Image Analyst
am 27 Jul. 2016
I don't understand. Do you not trust the users to pick the proper files? If not, then you can use dir() to specify the proper file pattern once they've chosen the folder via any of the other functions (uigetdir, uigetfile, or uipickfiles).
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!