Hi all, I'm new in Matlab and I would like to ask you help about a problem that I'm facing. The problem is how to save variables inside a parfor loop. I have looked up on internet and, as suggested in many forums, I have already tried to save them inside an external function (parsave) called inside parfor loops, but it doesn't work. This is my code:
for Es_N0_index=1:length(Es_N0_vec)
hyp1_count_var = 0;
hyp0_count_var = 0;
Detected_frame_var = 0;
FA_frame_var = 0;
parfor noise_iter = 1:NOISE_ITER
hypothesis_used=randi(2)-1;
if(hypothesis_used)
hyp1_count_var = hyp1_count_var + 1;
end
if(not(hypothesis_used))
hyp0_count_var = hyp0_count_var + 1;
end
...
if( (strcmp(Demod_State,'Frame_Sync')) && CorrectTimeSync && hypothesis_used)
Detected_frame_var=Detected_frame_var + 1;
elseif( strcmp(Demod_State,'Frame_Sync') && (~hypothesis_used))
FA_frame_var=FA_frame_var + 1;
else
disp('Final Case')
end
if (rem(noise_iter,10)==0)
disp('saving state..')
parsave(sprintf('output%d.mat', Es_N0_index),Detected_frame_var, FA_frame_var, hyp1_count_var, hyp0_count_var);
end
end
...
end
and the external function is:
function parsave(fname,Detected_frame_var, FA_frame_var, hyp1_count_var, hyp0_count_var)
parsave(sprintf('output%d.mat', Es_N0_index),Detected_frame_var, FA_frame_var,hyp1_count_var,hyp0_count_var);
end
What makes me crazy is that, if I comment the "if condition" where the parsave function is called (if(rem...)==...), everything works fine. Instead, if I uncomment these 3 lines,I get an error:
"Error using FS_Stand_Alone (line 62) An UndefinedFunction error was thrown on the workers for 'hyp1_count_var'. This might be because the file containing 'hyp1_count_var' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details.
Caused by: Undefined function or variable 'hyp1_count_var'. "
Do you know what I can do to fix it? Thanks