parallel processing - MATLAB: Copying Global variable for all Workers -
i want pass "a" global variable function "tsfn", say
function [ out ] = tsfn( ) global a; out=a+1; end
when run following expected result:
>> global a; a=1 out=[]; i=1:4 out =[out tsfn()]; end out = 1 out = 2 2 2 2
however if run parfor instead of end blank vector. leads me believe "a" not being passed function. i'm wondering if there way pass variable global variable workers.
thanks
works fine on platform. try restarting computer or matlab. generally, "parfor" loop accomplishes same task "for" loop--each loop computed in parallel. declaring "global" in 1 or more functions and/or base workspace allows each of them access contents of global variable, usage correct.
here code:
function[ out ] = tsfn() global a; out = + 1; end edu>> global a; edu>> = 1; edu>> out = []; edu>> parfor = 1 : 4 out = [ out tsfn() ]; end edu>> = 1 edu>> out out = 2 2 2 2
aside, simple way test contents of variable inside function remove semicolon, prints editor.
Comments
Post a Comment