Transparency violation error. Using 'eval' in a 'parfor' loop
Show older comments
Hello,
I am new to coding
I am trying to speed up the processing of my code by suing parfor loop however there are 2 eval functions in it
Here's the complete code;
tic
EventName = 'Hypopnea';
CHN = 'ECG';
fre = 200;
Felist = 'signal_tsallisEntropy';
Nor = 'Normalize_Whole_signal'; % Normalize_Whole_signal
Dset = 'visit1'; % visit3
Mpath = 'F:\Dataset';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
osp1 = '2_Features';
sp1 = 'MAT files';
sec = 30;
level = 5;
sp2 = strcat(Mpath,'\',sp1,'\',Dset);
load([sp2,'\RST.mat'])
load(['Event_List.mat'])
Ledf = RST.(CHN).(['fre_',num2str(fre)]);
TSNor = '';
if strcmp(Nor,'Normalize_Whole_signal')
TSNor = '_Normalize';
end
% Xfeat = table;
parfor i = 1:size(Ledf,1)
Cname = char(Ledf(i,1));
LP1 = strcat(sp2,'\',Cname,'_info.mat');
LP2 = strcat(sp2,'\',Cname,'_rec.mat');
CR_info = load(LP1);
CR_data = load(LP2); %%%%%
[id1,id2] = ismember(CHN,CR_info.hdr.label);
SleepEpochs = size(CR_info.y.SleepStages,2); % available sleep epochs
EVstruct = CR_info.y.ScoredEvents; % 1 by n : struct || Events
szREC = size(CR_data.record,2); % record size
MaxFre = max(CR_info.hdr.frequency); % max available frequency
REpoch = min(SleepEpochs,szREC/MaxFre/sec);
REC = CR_data.record(id2,1:(REpoch*sec*fre));
if strcmp(Nor,'Normalize_Whole_signal')
REC = Z_score_mat(REC);
end
REC = reshape(REC,(sec*fre),REpoch)';
LRST = Event_label(sec,REpoch,EVstruct,TE,TE_U);
Label = cell(LRST.Total,1);
Label(:,1) = {'Normal'};
eval(['Label(LRST.',EventName,',1) = {EventName};']);
eval(['[tpl1,tpl2] = ismember(LRST.',EventName,',LRST.ALL_Event);'])
USls = LRST.ALL_Event;
USls(tpl2,:) = [];
REC(USls,:) = [];
Label(USls,:) = [];
sp3 = strcat(Mpath,'\',osp1,'\',Dset,'\',EventName,'\',CHN,...
'_fre_',num2str(fre),TSNor,'\',Cname);
if isfolder(sp3) == 0
mkdir(sp3)
end
feat_mat = WDecBi_1(REC,level,Felist,i);
MTab = table(feat_mat,Label);
mysave([sp3,'\',Felist,'.mat'],MTab)
% YC = load([sp3,'\',Felist,'.mat']);
% Xfeat = cat(1,Xfeat,YC.MTab);
clc
disp(i)
toc
end
function mysave1(fName, MTab)
save(fName,'MTab');
end
Can anybody please find an alternative to eval function in parfor loop
I'd be glad to receive any help
Thank you.
2 Comments
Jonas
on 12 May 2021
use real code instead of eval function. smth like
Label(LRST.(EventName),1)=...
similarly in the other case. it is possible to use the dot expression with a variable, but you have to put parenthesis around the variable behin the dot like
varname='firstThing';
myStruct.(varname)
Stephen23
on 12 May 2021
"I am new to coding ... however there are 2 eval functions in it"
Avoid eval for trivial code like this:
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!