Transparency violation error. Using 'eval' in a 'parfor' loop
3 次查看(过去 30 天)
显示 更早的评论
Giridhar sai pavan kumar Konduru
2021-5-12
评论: Giridhar sai pavan kumar Konduru
2021-5-15
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 个评论
Jonas
2021-5-12
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
2021-5-12
"I am new to coding ... however there are 2 eval functions in it"
Avoid eval for trivial code like this:
采纳的回答
Jan
2021-5-12
Some general hints:
- load() without catching its output to a variable creates variables dynamically in the workspace. This impedes Matlab's JIT acceleration. In consequence loops can need up to 100 times more processing time. Another effect is that it impedes the debugging. I cannot guess, which variables are contained in the files. Therefore it is much harder to deside if some calls are functions or indexed variables.
load([sp2,'\RST.mat']) % RST = load(fullfile(sp2,'RST.mat'))
load(['Event_List.mat']) % Event = load('Event_List.mat');
- Use fullfile() instead of strcat, because it is safer and cares about the operating system automatically.
sp2 = fullfile(Mpath, sp1, Dset); % strcat(Mpath,'\',sp1,'\',Dset);
- Use braces instead of CHAR to get a char from a cell string:
Cname = Ledf{i}; % char(Ledf(i,1))
- ISMEMBER is an overkill for comparing a single char vector:
id2 = find(strcmp(CHN, CR_info.hdr.label));
- Avoid EVAL like hell. It makes the debugging horrible and slows down the processing massively again by craeting variables dynamically:
% eval(['Label(LRST.',EventName,',1) = {EventName};']);
Label{LRST.(EventName), 1} = EventName;
% eval(['[tpl1,tpl2] = ismember(LRST.',EventName,',LRST.ALL_Event);'])
[tpl1, tpl2] = ismember(LRST.(EventName), LRST.ALL_Event);
% I cannot test this, but this should be enough to demonstrate how to avoid
% EVAL.
- Omit the clc, because it wastes time only.
- There is no reason to call save in a specific subfunction mysave.
mysave(fullfile(sp3,, [Felist, '.mat'], 'MTab'); % Working directly
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!