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

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

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

Thank you @Bob Thompson!
I think I still have some doubts about how to incorporate that in that code that I have already developed... I wanted to be the least hardcoded as possible, do you have any ideas for how to put that information that you've said in that code?
The code I wrote out would go at the very beginning of your code (you will likely need to adjust it to perfectly fit your setup, it was for more for reference), where my 'dinfo' definition would replace your definition.
From there you should just need to add an if statement to select the various appropriate metric code blocks based on your received input.
if metrics == 1
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);
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;
elseif metrics == 2
...
...
I think I got this now, thank you so much for your time and help @Bob Thompson!!
Sorry for bother you once again @Bob Thompson but I have some questions about how to let the user choose which subfolder to run out of 16 present in the main folder or run through all of them...
I wanted to be the least hardcoded as possible, so can I do the same as you mentioned for the selection of the desired metric (using input) or is too much time consuming? Or there is an easier way to do it?
Thanks in advance!
You can have the user select the folder with the uigetdir command, which will open a window similar to opening a file through the MATLAB toolbar. My previous bit of code did this.
Alternatively, you could use the sprintf and input commands to select the appropriate selection from your previous dir variable.
fpath = uigetdir('Please select the parent folder for all data files.');
dinfo = dir([fpath]); % Use dir([fpath,'\*.mat']) for when selecting child folder only
dinfo = dinfo([dinfo(:).isdir]==1);
dinfo = dinfo(3:end);
Opt_str = 'Please select a folder to examine: \n';
for i = 1:length(dinfo)
Opt_str = sprintf([Opt_str, 'Enter %i for %s folder.\n'],i,dinfo(i).name);
end
fi = input(Opt_str);
flist = dir([dinfo(fi).folder,'\',dinfo(fi).name,'\*.mat']);
Depending on which path you take, you should end up with a list of .mat files in the chosen folder, which you can then run your loop through.
Oh man... thank you so much @Bob Thompson!!! 🙏
Hello @Bob Thompson! I'm so sorry for bother you again but it's the last time I promise!
When I run the code below, as you presented me, after the calculation be performed, my FC_CorrelationMatrix_BCorrD, FC_CorrelationMatrix_BCorrU, FC_CorrelationMatrix_PCorrD and FC_CorrelationMatrix_PCorrU only store the calculation of the last iteration performed but when I create these matrices (FC_CorrelationMatrix_BCorrD, FC_CorrelationMatrix_BCorrU, FC_CorrelationMatrix_PCorrD and FC_CorrelationMatrix_PCorrU) outside the loop it works just fine... do you know the reason for this?
for ...
if metrics == 1
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);
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;
elseif metrics == 2
...
...
Thank you once again!
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.
Thank you for the explanation Bob!! Now I get it!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 25 Feb 2021

Commented:

on 1 Mar 2021

Community Treasure Hunt

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

Start Hunting!