User input file directory

조회 수: 28 (최근 30일)
Bob Thompson
Bob Thompson 2016년 7월 26일
댓글: Bob Thompson 2016년 8월 1일
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.

채택된 답변

Image Analyst
Image Analyst 2016년 7월 27일
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
Image Analyst
Image Analyst 2016년 7월 28일
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?
Bob Thompson
Bob Thompson 2016년 8월 1일
At the time of writing my previous comment I was unaware of the 'fullfile' command. So yes, I was just trying to pass the folder into the dir() command. The code you have written would work fine, possibly better than what I have ended up with, and the only real reason I didn't end up using it was because I didn't know enough at the time that I wrote that section of the script.
Thank you for following up on this, and feel free to let me know if you have any other bits of advice.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 26일
Use uigetdir

Bob Thompson
Bob Thompson 2016년 7월 27일
Thanks for the responses.
'uigetdir' is really nice for selecting the directory, however, I did not see the ability to select only certain file types inside of a directory, which caused the resulting structured array to have an incorrect number of files.
'uipickfiles' seems to work better since I can output my file selections as a structured array, however, when I select my files the resulting array contains a number of extra cells relating to the files and folders in the directory the gui first opened. Is there some setting I missed that I need to turn off? I only selected the files I wanted, and my final directory wasn't anywhere near the original, but it still keeps creating selections for the original directory.
Used the code:
files_in = uipickfiles('out','struct');
  댓글 수: 4
Image Analyst
Image Analyst 2016년 7월 27일
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).
Bob Thompson
Bob Thompson 2016년 7월 28일
The problem I have been encountering is not with the user's ability, but with each of the functions.
If I use file_in = uigetdir and then dir(file_in) then I am unable to call only .dat files. This would not be a problem if I knew exactly what all the files are in the directory, but since I will not be the future user, I do not know all the files, and do not trust that they will all be in the right order for me to simply tell the script to ignore certain entries ( such as the '.' and '..' files).
If I use the uigetfile input, matlab formats the entries as [file1name, file2name, file3name] which cannot be read by fopen, because fopen seems to need the format ['file1name','file2name','file3name']. Therefore, I used the code in my previous comment to convert the forms.
Finally, using uipickfiles gave me similar problems to dir(), where matlab decided to include all of the files in my current directory into the resulting array, instead of just the files that were actually picked by the user. Once again, since I will not always be the user, I cannot control these extra files and needed to find a way around them.
So, no it's not that I don't trust the user, but I'm trying to prevent the user from being required to change the code in order to get the basic results they want.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by