【发布时间】:2016-10-23 23:01:15
【问题描述】:
在parfor-loop 的主体中调用的函数中使用evalin 时遇到问题。函数如下:
function returnData = extractFun(input)
% assign close price to function call
x = evalin('base','data');
% extract prices
returnData = x(input);
end
调用函数的脚本如下所示:
% 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
当我在 parfor-loop 中运行脚本时,matlab 会抛出一个错误,即索引超出了矩阵维度,这一定意味着变量 x 为空(或未正确评估)。奇怪的是,当我以正常的for-loop 运行循环时,一切正常。我知道parfor-loops 的透明度问题,因此我将evalin 放入一个单独的函数中。
我也愿意接受其他解决方案来解决我的问题,即在不使用额外循环的情况下,从 One 数据变量中提取数据到一个 n×1 元胞数组 doubles,因为我打算运行这个迭代次数非常多的循环。
谁能帮帮我?谢谢!
【问题讨论】: