avoid dinamic variable names in for loops

조회 수: 2 (최근 30일)
Enzo
Enzo 2023년 4월 5일
편집: Stephen23 2023년 4월 5일
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
Enzo
Enzo 2023년 4월 5일
@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 2023년 4월 5일
편집: Stephen23 2023년 4월 5일
"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

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by