avoid dinamic variable names in for loops

2 views (last 30 days)
Enzo
Enzo on 5 Apr 2023
Edited: Stephen23 on 5 Apr 2023
Hello everyone,
I am having trouble in creating variables names in a seemingly "reasonable" way. I know the principle behind (no eval, no dinamic variable name, etc), but i can't figure out how to solve the issue.
Someone among you could even argue against the use of for loop. In that case, please provide an alternative solution using matrix indexing.
The rationale behind this script is to extract powers band values from a LFP (similar to an EEG signal). TD_32 is a 3D matrix.
The signal was captured by a linear probe having 32 channels. This big picture: I deliver a stimulus. The signal being captured is a continuos time series. As a pre-processing steps, i had cut the time series in order to have a 3d matrix, where the third dimension are the different repetition (epochs), the second the channels which collect the signal from different sources and the first dimension being the actual signal.
I need to get the powers results (stored in different bands width as shown in the code) from all the channels and store them into different variables, or a matrix (i don't know what the best solution could be)
F = 0.5:0.1:150; %frequency range in Hz
delta = [0.5 4];
theta = [4 8];
alpha = [8 12];
beta = [12 25];
gamma = [25 80];
for ch=10:29
matr_500_post = mean(TD_32(:,ch,:),3);
[pxx_n_new_4,hz_n_new_4] = pwelch(matr_500_post,window,[],F, srate); % post stim
[~,fidx(1)] = min(abs(hz_n_new_4-delta(1) ));
[~,fidx(2)] = min(abs(hz_n_new_4-delta(2) ));
avpow_delta = mean(pxx_n_new_4(fidx(1):fidx(2)));
[~,fidx(1)] = min(abs(hz_n_new_4-theta(1) ));
[~,fidx(2)] = min(abs(hz_n_new_4-theta(2) ));
avpow_theta = mean(pxx_n_new_4(fidx(1):fidx(2)));
[~,fidx(1)] = min(abs(hz_n_new_4-alpha(1) ));
[~,fidx(2)] = min(abs(hz_n_new_4-alpha(2) ));
avpow_alpha = mean(pxx_n_new_4(fidx(2):fidx(2)));
[~,fidx(1)] = min(abs(hz_n_new_4-beta(1) ));
[~,fidx(2)] = min(abs(hz_n_new_4-beta(2) ));
avpow_beta = mean(pxx_n_new_4(fidx(1):fidx(2)));
[~,fidx(1)] = min(abs(hz_n_new_4-gamma(1) ));
[~,fidx(2)] = min(abs(hz_n_new_4-gamma(2) ));
avpow_gamma = mean(pxx_n_new_4(fidx(1):fidx(2)));
powers_500ms_post = [avpow_delta,avpow_theta,avpow_alpha,avpow_beta,avpow_gamma];
end
  4 Comments
Enzo
Enzo on 5 Apr 2023
@Mathieu NOE I am not sure how in this scenario. I have reviewed stephen23 detailed explanation but failed to find something suitable. I am sure I am missing something very basic
Stephen23
Stephen23 on 5 Apr 2023
Edited: Stephen23 on 5 Apr 2023
"I am sure I am missing something very basic"
Perhaps a very basic cell array:
V = 10:29;
C = cell(size(V));
for k = 1:numel(V)
ch = V(k);
... your code here
C{k} = powers_500ms_post;
end

Sign in to comment.

Answers (0)

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!