How can I allow the user to choose what to do?

14 views (last 30 days)
Hello everyone!!
I have a main forder D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal with 16 subfolders (ie Stanford, KKI, Leuven, Trinity, etc...). With this code below I perform 6 different calculations (Correlation, h2, mutual information, transfer entropy, coherence and granger) for all the subjects present in all subfolders and then save these matrices generated.
But, I came up with the idea of letting the user choose which metric to analyze (and the option to analyze all) and for which subfolder... Can anyone give me some help in this?
%% Load files from main folder subfolders
dinfo = dir('D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal\**\*.mat');
filenames = fullfile({dinfo.folder}, {dinfo.name});
numfiles = length(filenames);
vect_NumberSubjs = [15 23 24 33 36 36 37 39 41 44 48 59 61 75 113 169];
vect_TR = [repelem(2,37) repelem(2.5,39) repelem(1.6,59) repelem(3,41) repelem(2,169) repelem(2.5,23) repelem(1.5,24) repelem(1.5,36) repelem(2.2,15) repelem(2,33) repelem(2,36) repelem(2,44) repelem(3,75) repelem(2,113) repelem(2,61) repelem(2,48)];
NumberSubjs = sum(vect_NumberSubjs);
%% Parameters for metrics
modelOrder = 2;
bins = 2;
params = struct('MaxDelay', 10, 'bins', 10);
maxlag = 10;
freq_min = 0.01;
freq_max = 0.08;
freqs = [freq_min freq_max];
%% Store each metric in the respective matrix
FC_CorrelationMatrix_BCorrD = zeros(116,116,NumberSubjs);
FC_CorrelationMatrix_BCorrU = zeros(116,116,NumberSubjs);
FC_CorrelationMatrix_PCorrD = zeros(116,116,NumberSubjs);
FC_CorrelationMatrix_PCorrU = zeros(116,116,NumberSubjs);
FC_h2Matrix_BH2D = zeros(116,116,NumberSubjs);
FC_h2Matrix_BH2U = zeros(116,116,NumberSubjs);
FC_h2Matrix_PH2D = zeros(116,116,NumberSubjs);
FC_h2Matrix_PH2U = zeros(116,116,NumberSubjs);
FC_MIMatrix_BMITD1 = zeros(116,116,NumberSubjs);
FC_MIMatrix_BMITD2 = zeros(116,116,NumberSubjs);
FC_MIMatrix_PMITD1 = zeros(116,116,NumberSubjs);
FC_MIMatrix_PMITD2 = zeros(116,116,NumberSubjs);
FC_MIMatrix_BMITU = zeros(116,116,NumberSubjs);
FC_MIMatrix_PMITU = zeros(116,116,NumberSubjs);
FC_TEMatrix_BTED = zeros(116,116,NumberSubjs);
FC_TEMatrix_BTEU = zeros(116,116,NumberSubjs);
FC_TEMatrix_PTED = zeros(116,116,NumberSubjs);
FC_TEMatrix_PTEU = zeros(116,116,NumberSubjs);
FC_CohMatrix_BCohF1 = zeros(116,116,NumberSubjs);
FC_CohMatrix_BCohF2 = zeros(116,116,NumberSubjs);
FC_CohMatrix_BCohW1 = zeros(116,116,NumberSubjs);
FC_CohMatrix_BCohW2 = zeros(116,116,NumberSubjs);
FC_CohMatrix_PCohF1 = zeros(116,116,NumberSubjs);
FC_CohMatrix_PCohF2 = zeros(116,116,NumberSubjs);
FC_CohMatrix_PCohW1 = zeros(116,116,NumberSubjs);
FC_CohMatrix_PCohW2 = zeros(116,116,NumberSubjs);
FC_GCMatrix_GC = zeros(116,116,NumberSubjs);
FC_GCMatrix_PGC = zeros(116,116,NumberSubjs);
FC_GCMatrix_CondGC= zeros(116,116,NumberSubjs);
%% Calculate metrics
for i=1:numfiles
thisfile = filenames{i};
thisfile_cell = struct2cell(load(thisfile));
lfp = transpose(thisfile_cell{1}); % it's always changing
TR = vect_TR(i); % it changes for every subfolder
fs = 1/TR;
Corr_Methods = mln_icalcMatTimeBasic(lfp,modelOrder);
FC_CorrelationMatrix_BCorrD(:,:,i) = Corr_Methods.BCorrD;
FC_CorrelationMatrix_BCorrU(:,:,i) = Corr_Methods.BCorrU;
FC_CorrelationMatrix_PCorrD(:,:,i) = Corr_Methods.PCorrD;
FC_CorrelationMatrix_PCorrU(:,:,i) = Corr_Methods.PCorrU;
h2_Methods = mln_icalcMatH2(lfp,modelOrder,bins);
FC_h2Matrix_BH2D(:,:,i) = h2_Methods.BH2D;
FC_h2Matrix_BH2U(:,:,i) = h2_Methods.BH2U;
FC_h2Matrix_PH2D(:,:,i) = h2_Methods.PH2D;
FC_h2Matrix_PH2U(:,:,i) = h2_Methods.PH2U;
MI_Methods = mln_icalcMatMITime(lfp,params);
FC_MIMatrix_BMITD1(:,:,i) = MI_Methods.BMITD1;
FC_MIMatrix_BMITD2(:,:,i) = MI_Methods.BMITD2;
FC_MIMatrix_PMITD1(:,:,i) = MI_Methods.PMITD1;
FC_MIMatrix_PMITD2(:,:,i) = MI_Methods.PMITD2;
FC_MIMatrix_BMITU(:,:,i) = MI_Methods.BMITU;
FC_MIMatrix_PMITU(:,:,i) = MI_Methods.PMITU;
TE_Methods = mln_icalcMatTE(lfp,maxlag);
FC_TEMatrix_BTED(:,:,i) = TE_Methods.BTED;
FC_TEMatrix_BTEU(:,:,i) = TE_Methods.BTEU;
FC_TEMatrix_PTED(:,:,i) = TE_Methods.PTED;
FC_TEMatrix_PTEU(:,:,i) = TE_Methods.PTEU;
Coh_Methods = mln_icalcMatFreqBasic(lfp,freqs,fs);
FC_CohMatrix_BCohF1(:,:,i) = Coh_Methods.BCohF(:,:,1);
FC_CohMatrix_BCohF2(:,:,i) = Coh_Methods.BCohF(:,:,2);
FC_CohMatrix_BCohW1(:,:,i) = Coh_Methods.BCohW(:,:,1);
FC_CohMatrix_BCohW2(:,:,i) = Coh_Methods.BCohW(:,:,2);
FC_CohMatrix_PCohF1(:,:,i) = Coh_Methods.PCohF(:,:,1);
FC_CohMatrix_PCohF2(:,:,i) = Coh_Methods.PCohF(:,:,2);
FC_CohMatrix_PCohW1(:,:,i) = Coh_Methods.PCohW(:,:,1);
FC_CohMatrix_PCohW2(:,:,i) = Coh_Methods.PCohW(:,:,2);
GC_Methods = mln_icalcMatGranger(lfp,modelOrder);
FC_GCMatrix_GC(:,:,i) = GC_Methods.GC;
FC_GCMatrix_PGC(:,:,i) = GC_Methods.PGC;
FC_GCMatrix_CondGC(:,:,i) = GC_Methods.CondGC;
end
%% Store the results
MatricesCalculationDir = 'D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal';
save(fullfile(MatricesCalculationDir, 'FC_CorrelationMatrix_BCorrD'), 'FC_CorrelationMatrix_BCorrD');
save(fullfile(MatricesCalculationDir, 'FC_CorrelationMatrix_BCorrU'), 'FC_CorrelationMatrix_BCorrU');
save(fullfile(MatricesCalculationDir, 'FC_CorrelationMatrix_PCorrD'), 'FC_CorrelationMatrix_PCorrD');
save(fullfile(MatricesCalculationDir, 'FC_CorrelationMatrix_PCorr U'), 'FC_CorrelationMatrix_PCorrU');
save(fullfile(MatricesCalculationDir, 'FC_h2Matrix_BH2D'), 'FC_h2Matrix_BH2D');
save(fullfile(MatricesCalculationDir, 'FC_h2Matrix_BH2U'), 'FC_h2Matrix_BH2U');
save(fullfile(MatricesCalculationDir, 'FC_h2Matrix_PH2D'), 'FC_h2Matrix_PH2D');
save(fullfile(MatricesCalculationDir, 'FC_h2Matrix_PH2U'), 'FC_h2Matrix_PH2U');
save(fullfile(MatricesCalculationDir, 'FC_MIMatrix_BMITD1'), 'FC_MIMatrix_BMITD1');
save(fullfile(MatricesCalculationDir, 'FC_MIMatrix_BMITD2'), 'FC_MIMatrix_BMITD2');
save(fullfile(MatricesCalculationDir, 'FC_MIMatrix_PMITD1'), 'FC_MIMatrix_PMITD1');
save(fullfile(MatricesCalculationDir, 'FC_MIMatrix_PMITD2'), 'FC_MIMatrix_PMITD2');
save(fullfile(MatricesCalculationDir, 'FC_MIMatrix_BMITU'), 'FC_MIMatrix_BMITU');
save(fullfile(MatricesCalculationDir, 'FC_MIMatrix_PMITU'), 'FC_MIMatrix_PMITU');
save(fullfile(MatricesCalculationDir, 'FC_TEMatrix_BTED'), 'FC_TEMatrix_BTED');
save(fullfile(MatricesCalculationDir, 'FC_TEMatrix_BTEU'), 'FC_TEMatrix_BTEU');
save(fullfile(MatricesCalculationDir, 'FC_TEMatrix_PTED'), 'FC_TEMatrix_PTED');
save(fullfile(MatricesCalculationDir, 'FC_TEMatrix_PTEU'), 'FC_TEMatrix_PTEU');
save(fullfile(MatricesCalculationDir, 'FC_CohMatrix_BCohF1'), 'FC_CohMatrix_BCohF1');
save(fullfile(MatricesCalculationDir, 'FC_CohMatrix_BCohW1'), 'FC_CohMatrix_BCohW1');
save(fullfile(MatricesCalculationDir, 'FC_CohMatrix_PCohF1'), 'FC_CohMatrix_PCohF1');
save(fullfile(MatricesCalculationDir, 'FC_CohMatrix_PCohW1'), 'FC_CohMatrix_PCohW1');
save(fullfile(MatricesCalculationDir, 'FC_CohMatrix_BCohF2'), 'FC_CohMatrix_BCohF2');
save(fullfile(MatricesCalculationDir, 'FC_CohMatrix_BCohW2'), 'FC_CohMatrix_BCohW2');
save(fullfile(MatricesCalculationDir, 'FC_CohMatrix_PCohF2'), 'FC_CohMatrix_PCohF2');
save(fullfile(MatricesCalculationDir, 'FC_CohMatrix_PCohW2'), 'FC_CohMatrix_PCohW2');
save(fullfile(MatricesCalculationDir, 'FC_GCMatrix_GC'), 'FC_GCMatrix_GC');
save(fullfile(MatricesCalculationDir, 'FC_GCMatrix_PGC'), 'FC_GCMatrix_PGC');
save(fullfile(MatricesCalculationDir, 'FC_GCMatrix_CondGC'), 'FC_GCMatrix_CondGC');

Accepted Answer

Bob Thompson
Bob Thompson on 25 Feb 2021
Some basic ways to get input from the user include commands such as uigetfile, uigetdir, and input. I recommend the last, coupled with a piece of logic to check the answer, in order to select the type of metric you want to use. The others can be used to select the appropriate files.
disp(['Please select the desired metric:\n ' ...
'Enter "1" for Correlation\n' ...
'Enter "2" for .........' % enter rest of options, I'm lazy
'Enter "7" for all metrics.\n')
metrics = input('Desired metric selection: ');
fpath = uigetdir('Select the folder containing the desired files.');
dinfo = dir([fpath,'\**\*.mat']);
  9 Comments
Bob Thompson
Bob Thompson on 28 Feb 2021
It's happening because you're creating the matrix every time you run the loop.
1) for loop begins; i == 1
2) metrics == 1 so it does the following:
FC_CorrelationMatrix_BCorrD = zeros(116,116,NumberSubjs);
% and
FC_CorrelationMatrix_BCorrD(:,:,i) = Corr_Methods.BCorrD;
3) end of for loop calculations, time to begin again; i == 2
4) metrics == 1 so it does the following:
FC_CorrelationMatrix_BCorrD = zeros(116,116,NumberSubjs); % This overwrites the previous values with zeros
% and
FC_CorrelationMatrix_BCorrD(:,:,i) = Corr_Methods.BCorrD; % Sheet two is the only thing now filled out
The solution isn't elegent, but you can either move the if statement, and matrix generation outside the loop, and have a series of small loops (one for each if option), or make two sets of if statements. The first will be before the loop, containing matrix generation, and then the loop, with the same internal if statement, but only containing the sheet values generation command, and the sheet assignment.
Iugo
Iugo on 1 Mar 2021
Thank you for the explanation Bob!! Now I get it!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!