Opening A directory of folders and accessing data within each folder

73 ビュー (過去 30 日間)
Mary
Mary 2014 年 1 月 17 日
コメント済み: Image Analyst 2018 年 8 月 9 日
Good Morning
I'm trying to write a batch code which does the following:
Open directory In a loop open each folder in the directory In a loop open each file with a '.res' file type in the folder Perform functions on each file Plot the data from each file Plot the data on top of the file within each folder
Plot the data from the next folder in a different figure
So far I have something like this, although its not really working
mainFolder = dir(foldername);
figNum = 1;
for k = 1:numel (mainFolder.name)
subFolder = dir(mainFolder(k).name,'*.res')
for j = 1:numel (subFolder.name)
rawData = importdata(subFolder(j).name,' ');
timeData = rawData(:,1);
ampData = rawData(:,2);
figure(figNum)
hold on
plot (timeData,ampData)
%other function stuff
end
hold off
figNum = figNum +1;
end
end
Any input would be greatly appreciated!

採用された回答

Image Analyst
Image Analyst 2014 年 1 月 17 日
Attached is s simple script which recurses into a folder and all subfolders of it looking for files of a certain filename pattern.
  3 件のコメント
Brian Droncheff
Brian Droncheff 2018 年 8 月 9 日
I did notice that there was an issue with the semi colon ';' needing to be a colon ':' for parsing to work in strtok e.g. instead of strtok(remain, ';'); One needs to have strtok(remain, ':');
Very nice script though - it helped a lot!!
Image Analyst
Image Analyst 2018 年 8 月 9 日
Brian, sorry - you're incorrect -- semicolon is what you want, not colon. Just look at the remain variable and you'll see that a semicolon is what delimits folder names. The colon is after drive letters and is not a delimiter - it's part of the full path of the folder.
Anyway, in newer versions they have strsplit, as well as a dir() that will recurse subfolders on its own. So my code I posted here is obsolete.

サインインしてコメントする。

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2014 年 1 月 17 日
Both for-loop signatures look like the following, which is likely invalid.
for k = numel mainFolder.name
What you likely want is
for k = 1:numel(mainFolder)
I.e. the number of folders you wish to loop over (the size of the mainFolder struct).
  3 件のコメント
Image Analyst
Image Analyst 2014 年 1 月 17 日
Copy and paste is helpful to avoid problems like that.
Mary
Mary 2014 年 1 月 17 日
You're right - I thought it would be too cluttered posting my commented code so I just rewrote it in english and in fake code.
I'll most likely copy/paste in the future so I can immediately run edits from the command prompt.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by