フィルターのクリア

Looping through folders and perform script

2 ビュー (過去 30 日間)
PAK
PAK 2018 年 8 月 30 日
コメント済み: PAK 2018 年 10 月 17 日
Hi All,
I have data that is in 3 subdirectories "LFP", "Pos", and "Neg". Each folder has the same number of .mat files in it, say 8 for this example. I have an analysis script that I have written that works. Now I would like to loop through all the files in each of the folders and run the analysis and save the output.
It would look something like:
load Pos/times_NS6_001.mat
load LFP/NS3.001.mat
% perform body of script %
% then save output %
savename = fullfile(char(strcat('Phase_Locking_','NS6_001','NS3_001')));
save(savename,'polarhistograms', 'pval', 'z');
Then I would loop through and do the same for Pos/times_NS6_00(2-8) and LFP/NS3.00(2-8). After this it would be the same for Neg/times_NS6_00(1-8) and LFP/NS3.00(1-8).
I need to basically test every file in the LFP folder with every file in both the "Pos" and "Neg" folders (all combinations).
Any help on what to add to my code to get this done in an efficient way would be much appreciated!
PAK
  3 件のコメント
PAK
PAK 2018 年 9 月 12 日
Hi Rik,
I'm still working through this code, got a little bit busy with another project. I will reply once I figure it out.
Thanks for your comment and followup!
PAK
PAK
PAK 2018 年 10 月 17 日
This worked perfectly! Thank you so much!
PAK

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

採用された回答

Rik
Rik 2018 年 8 月 30 日
編集済み: Rik 2018 年 8 月 31 日
  1. Use a dir call to figure out the number of suffixes (or suffices?)
  2. Use two nested loops to determine the selection of your .mat files. You could even use the output by dir directly as you for loop array (just transpose the struct).
pos_files=dir('Pos/times_*.mat');
neg_files=dir('Neg/times_*.mat');
lfp_files=dir('LFP/NS3.*.mat');
for pos_or_neg=[pos_files;neg_files]'
load([pos_or_neg.folder filesep pos_or_neg.name])
for current_lfp=lfp_files'
load([current_lfp.folder filesep current_lfp.name])
% perform body of script %
% then save output %
savename = %you can use the name fields for naming%
save(savename,'polarhistograms', 'pval', 'z');
end
end
  2 件のコメント
Stephen23
Stephen23 2018 年 8 月 31 日
@Rik Wisselink: you missed some relevant parts of the question: "Each folder has the same number of .mat files in it, say 8 for this example" and "Then I would loop through and do the same for Pos/times_NS6_00(2-8) and LFP/NS3.00(2-8)": so the number of files can vary, and thus hard-coding NS6_001 and NS3_001 is not going to help.
Rik
Rik 2018 年 8 月 31 日
@Stephen As it wasn't clear to me what the naming scheme should be (as it would overlap between pos and neg), I left that line as it was. I now removed that to avoid confusion.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by