matlab using evalin in parfor
<p>I have an issue with using <code>evalin</code> within a function that is being called in the body of a <code>parfor-loop</code>. The function looks as follow:</p>

<pre><code>function returnData = extractFun(input)

    % assign close price to function call
    x = evalin('base','data');

    % extract prices
    returnData = x(input);

end
</code></pre>

<p>And the script calling the function looks like this:</p>

<pre><code>% data-array = n-by-1 double
data = [1:1000]';

% loop and extract data
parfor i = 1:10

    % n-by-1 cell array containing 1-by-x doubles
    % doubles in var1 contain valid indicies for the data-variable

    var1 = {[1:10]; [1:30]};

    % perform cell-function since, cell2mat will not work due to
    % inconsistent dimensions of the double arrays contained in the cells

    extractData = cellfun(@returnData,var1,'UniformOutput',false);

    % do something with extractData

end
</code></pre>

<p>When I run the script in a <code>parfor</code>-loop, matlab throws an error, that the index exceeds the matrix dimensions, which must mean that the variable <code>x</code> is empty (or not being evaluated correctly). The weird thing is, that when I run the loop as a normal <code>for</code>-loop, everything works fine. I know about the transparency-issues with <code>parfor</code>-loops, hence I put the <code>evalin</code> into a separate function.</p>

<p>I would also be open to alternative solutions to my problem, which is extracting data from One data variable into a n-by-1 cell array as <code>doubles</code> without the use of additional loops, as I am intending to run this loop with a very high number of iterations.</p>

<p>Can anyone help me out? Thanks!</p>