MATLAB Answers

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

조회 수: 9(최근 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
Image Analyst
Image Analyst 4 Apr 2018
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 3 Apr 2018
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 3 Apr 2018
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 3 Apr 2018
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
Stephen Cobeldick
Stephen Cobeldick 3 Apr 2018
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.

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

Community Treasure Hunt

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

Start Hunting!

Translated by