필터 지우기
필터 지우기

In parfor , why a variable cannot be classified?

조회 수: 1 (최근 30일)
sm
sm 2015년 7월 2일
답변: Walter Roberson 2015년 7월 3일
In the code below I get an error
Error: The variable Matrix in a parfor cannot be classified.See Parallel for Loops in MATLAB, "Overview".
parfor i=1:n
try
query = 'SELECT * FROM TABLE WHERE Criterion = '' criteria(i) ''';
curs = exec(conn, query);
curs = fetch(curs);
close(curs);
results = curs.Data;
A = f(results);
B = g(results);
C = h(results);
D = k(results);
Matrix(i,6:9) = horzcat(A,B,C,B);
catch
Matrix(i,6:9) = {0,0,0,0};
end
end
Any ideas what I am missing?
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 7월 2일
Do A, B, C, D come out as cell arrays? At the moment it looks plausible that you have storing a numeric vector in one case and a vector of cells in the other case.
sm
sm 2015년 7월 2일
Yes, A,B,C and D are all cells. Also, note that the parfor loop runs fine as a simple for loop.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2015년 7월 3일
write to
tMatrix(i,:) = horzcat(A,B,C,B);
or write {0,0,0,0} there. Then after the parfor, let
Matrix(:,6:9) = tMatrix;
The difficulty you are running into is that you are trying to update only part of one row of a matrix, which requires that the matrix be input to the parfor as well as output from the parfor. parfor can handle rows that are only input and rows that are only output, but it doesn't like input-update-output as your code is requiring. The adjusted version I show has the entire row of tMatrix be output, and that is fine with parfor.

추가 답변 (1개)

Brendan Hamm
Brendan Hamm 2015년 7월 2일
Preallocate your matrix before the loop:
Matrix = nan(n,9)
Although lookingat your catch statement is Matrix a cell array? That is confusing.
  댓글 수: 1
sm
sm 2015년 7월 2일
I do pre-allocate the cell array before the parfor loop
n = size(data,1);
Matrix = cell(n,14);
Matrix(:,1:5) = data;
parfor i=1:n
....
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by