Is it possible to let MATLAB choose a file in a path if the user enters the first letters belonging to the file?

조회 수: 3 (최근 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
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
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
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
Guillaume
Guillaume 2018년 4월 3일
편집: Guillaume 2018년 4월 3일
In addition to the link in Image Analyst's answer, another risk of cd'ing is that you can bring mat files in and out of scope. Expect hilarious results if one of these folders you cd into contains a m file with the same name as a built-in matlab function.

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

추가 답변 (1개)

ES
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
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
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 CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by