Is it possible to let MATLAB choose a file in a path if the user enters the first letters belonging to the file?
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
I am designing a user interface using Appdesigner. This UI needs to select a folder and add this to the path. After having done this, the user can enter the first letters of the file (for example: ID) and then enter the number of the file (for example 001, 010, 011....etc). How can I let MATLAB search the folder(path) for this specific file?
Any help would be appreciated!
4 个评论
Guillaume
2018-4-4
编辑:Guillaume
2018-4-4
In general, folders containing the data should not be added to the matlab path. Only the folders that contain the code to process such data.
If the processing code is written properly (i.e. it uses absolute paths) then it can process the data wherever it is. For example, all my data is loaded directly from a remote server.
Image Analyst
2018-4-4
No you don't. You can use the folder name and pattern (like *.dat or whatever) in dir and then use what dir returns to construct your filenames, like it shows in this other FAQ entry: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
No cd'ing is needed if you just need to open a file.
采纳的回答
Image Analyst
2018-4-3
I agree with the other two experts (Stephen and Guillaume).
Use dir() instead of dos() or you can use datastore().
And use fullfile() to create full file names rather than using cd(). See the FAQ: http://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function.
Get the string from the edit field (sorry I only know how to do it in GUIDE so far, not App Designer), then create the list of filenames.
filePattern = sprintf('%s*.*', prefix);
fileInfo = dir(filePattern);
1 个评论
更多回答(1 个)
ES
2018-4-3
I would use dos
dos(['dir /b /s ','ID*.jpg'])
This returns a string, which you can then parse by new line character.
function cFilesList = getListOfFiles(sFolderName, sFilesToCheck)
% Example inputs
% sFilesToCheck = '*.m,lib_*.mdl';
% sFolderName = 'u:\ECAS_CAR_SW\D07_Design\ASW\LC\XLS2DD';
sCurrDirectory = pwd;
cd(sFolderName);
% DOS to find the full file names
[~, sFilesList] = dos(['dir /b /s ',sFilesToCheck]);
cd(sCurrDirectory);
if isempty(strfind(sFilesList, 'File Not Found'))
% Get list of files by parsing the string
cFilesList = getFilesList(sFilesList);
else
cFilesList = {};
end
end
function cFilesList = getFilesList(sFilesList)
% sFileList is string input from DOS command
% Find all Line Feeds (\n - char(10))
iIdx = strfind(sFilesList, char(10));
% Files Count
iFilesCount = length(iIdx);
% Pre Allocation
cFilesList = cell(iFilesCount, 1);
iIdx = [0, iIdx];
% Based on the position of char(10), construct the file name and add to the
% cell output.
for iLoop = 1:iFilesCount
sFileFullName = sFilesList(iIdx(iLoop)+1:iIdx(iLoop+1)-1);
cFilesList{iLoop} = sFileFullName;
end
end
2 个评论
Guillaume
2018-4-3
编辑:Guillaume
2018-4-3
Matlab has its own dir function. It would be a lot more reliable than using dos which is windows only and will not work if the current directory is a UNC path.
Also, generally there are no reasons to pwd or cd anything. It is a lot more reliable to work with full paths rather than modifying the current directory which runs the risk of bringing in/out of scope functions.
Stephen23
2018-4-3
Avoid using cd in code: it makes debugging more difficult, changes which functions can run, and is much slower than using absolute/relative paths. Using relative/absolute paths is much more reliable.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!