How do I get cd to work properly?
12 次查看(过去 30 天)
显示 更早的评论
I want to create a code that will parse through 15 folders (X_1, X_2, X_3) and open a subfolder folder 'Y_*' in each of these fifteen folders and open one document in the directory (example: X_1/Y_1/*.coord) I can do this easily in the command window using cd and I am able to code a for loop that will parse through the main X folders but I cannot figure out a way to open a folder with only part of the string. All of the subfolders start with Y_ but end with different numbers. I want Matlab to open the folder regardless of what number it is and only examine 'Y_'
I have tried doing this but I always get an error. How do I write this?
cd X_1/Y_*
0 个评论
采纳的回答
Steven Lord
2022-6-15
The cd function doesn't accept wildcards. You could use dir (which does) and iterate through the list of directories in its output. I would avoid actually using cd to change directory; instead use fullfile to assemble the path to the file and then pass that path into whatever file reader you're using.
d = fullfile(matlabroot, 'toolbox', 'matlab', 'general')
howManyFilesAndDirs = numel(dir(d))
f = fullfile(d, 'bench.dat')
theText = fileread(f);
theLines = split(theText, newline);
theLines(1:5)
更多回答(1 个)
另请参阅
类别
在 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!