Loop over file and extracting information from excel spreeadsheet for calculation over those files

4 Ansichten (letzte 30 Tage)
Hello everyone, let me explain my code first. This make individual analysis of a file (.mat) based on information that I introduce manually, I would like to loop over the files and use automatically the information of each file that is contained in the spreadsheet.
Line 1 set the name of the file for analysis (this need to be looped file 1 to file n) and every filename is the same that is contained in the spreadsheet column I.
Line 3 calls a mat structure (called frames) that contain the same information that needs to be read from my spreadsheet. So far I have to do 1 .mat every time that I have to make these calculations.
Lines 4-6 calls information that need to be used from the spreadsheet. Line 4 specifies the row for the trial of my experiment. Lines 5 and 6 set the channels that I´m using for analysis, column M and N of the spreadsheet.
lines 15-17 use information from the spreadsheet from colum 1, 2 and 3 respectively, now in my spreadsheet these are J K and L
Finally, line 24 stores these results into a .mat structure called results and append to the end a row with the columns MI_A, MI_B, MI_C, AF_A, PF_A, AF_B, PF_B, AF_C, PF_C]
I imagine that is a double loop, one for read the file and the other for extracting the information from my spreadsheet, but honestly I don´t know how to implement that.
Thank you in advance.
file = 'TD26d1t1';
load(file);
load 'frames';
row = 1;% SELECT ROW FOR THE TRIAL
chn_1 = 4;% SELECT CHANNEL FOR PHASE (Low Freq)
chn_2 = 5;% SELECT CHANNEL FOR AMPLITUDE(High Freq)
lfp_A = LAN.data{1}(chn_2,:); %LFP for phase(HPC)
lfp_B = LAN.data{1}(chn_1,:); %LFP for amplitude(PFC)
srate = LAN.srate;
% Time windows in turn to LASER
time = LAN.time(1) + 1/srate:1/srate:LAN.time(2);
srate_vid = 30; %frames per second
t0 = 0;
frame_ini_intan = frames(row,1);
frame_ini_nav = frames(row,2);
frame_fin_nav = frames(row,3);
%Calculations based on previuos information
%% Final result: MI: modulation Index; AF: freq of max amplitude; PF: freq of max phase.
load 'Results'
MI_tot = [MI_A, MI_B, MI_C, AF_A, PF_A, AF_B, PF_B, AF_C, PF_C];
Resultados = [Results;MI_tot]
save 'Results'
  3 Kommentare
Sebastian Espinoza
Sebastian Espinoza am 13 Jan. 2022
Thank you @Mathieu NOE, so far I found a partial solution. First I transform my script into a function (I can call it for other projects), then I wrote a loop that search for the information needed in my spreadsheet and use it as function argument. But, apparently isn't working when I try to append my result in a final structure.
%% load spreadsheet
T= readtable("frames_control.xlsx");
%% looping function cf2areas
len = size(T)
for i=1:len(1)
name = char(T.fname(i))
results=cfc2areas(name,T.chan_1(i),T.chan_2(i),T.frame_ini_intan(i),T.frame_ini_nav(i),T.frame_fin_nav(i));
load 'Resultados'
Resultados = [Resultados;results]
save 'Resultados'
end
%%

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Chetan
Chetan am 27 Sep. 2023
Bearbeitet: Chetan am 27 Sep. 2023
I understand that you are experiencing issues while working with multiple files and saving data for each file. To address this, I suggest making the following modifications to your code:
  • Instead of repeatedly loading and saving the result file, you can optimize your code by creating an array outside the for loop.
  • After the loop, you can save the data by incorporating the necessary code changes.
Please find below the modified code:
T = readtable("frames_control.xlsx");
% Create an empty matrix to store the results
Resultados = [];
% Loop over the rows in the spreadsheet
for i = 1:size(T, 1)
% Get the file name from the spreadsheet
file = char(T.fname(i));
% Get the required information from the spreadsheet
chn_1 = T.chan_1(i);
chn_2 = T.chan_2(i);
frame_ini_intan = T.frame_ini_intan(i);
frame_ini_nav = T.frame_ini_nav(i);
frame_fin_nav = T.frame_fin_nav(i);
% Call the function to perform the calculations
results = cfc2areas(file, chn_1, chn_2, frame_ini_intan, frame_ini_nav, frame_fin_nav);
% Append the results to the final matrix
Resultados = [Resultados; results];
end
I hope these suggestions help you resolve the issue you are facing.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by