Error: The variable voltages1 in a parfor cannot be classified.

Hello,this is my code.
It is not runing because "The variable voltages1 in a parfor cannot be classified." Why?
voltages1 = num2cell(zeros(1,256),1);
voltages2 = voltages1;
parfor i=1:8
inputVoltage1 =readVoltage(a,'A0');
inputVoltage2 =readVoltage(a,'A5');
voltages1{256} = inputVoltage1;
voltages2{256} = inputVoltage2;
subplot(2,1,1) ;
[ax,h1,h2] = plotyy(times,voltages1{:},times,voltages2{:});
%axis(ax(1),[0 1 0 5]);
%axis(ax(2),[0 1 0 5]);
set(h1,'xdata',times,'ydata',voltages1{:});
set(h2,'xdata',times,'ydata',voltages2{:});
set(get(ax(1),'Ylabel'),'String','I Channel');
set(get(ax(2),'Ylabel'),'String','Q Channel');
title('Time Domain');
set(h1,'Linewidth',2);
set(h2,'Linewidth',2);
drawnow update;
voltages1{1:255} = voltages1{2:256};
voltages2{1:255} = voltages2{2:256};
end

 Accepted Answer

The problem is the line
voltages1{256} = inputVoltage1;
You cannot set a fixed index of an array or a matrix in a parfor loop, since multiple parallel instances of the loop will conflict with each other, trying to set voltages1{256} to different values at the same time.

4 Comments

Thanks for answering,Marc. But I have no idea what different values mean. Can you explain clearly? Thanks much
No problem :) I'll try to explain with an example.
x = [0; 0];
parfor i = 1:2
x(2) = i;
end
In a parfor loop, the iterations run in parallel (depending on how many threads your machine has). So for example, this one might run on two threads at the same time, so the operation x(2) = i is runs in parallel. If this were to run, Matlab would try to set x(2) = 1 and x(2) = 2 at the same time. But that's a conflict and can't be done. So to prevent this from happening, Matlab throws an error if you try to do something like this.
In your case, you are trying to set voltages{256} equal to readVoltage(a,'A0') within the parfor loop. This would not result in a conflict, because readVoltage(a,'A0') is the same in every instance. But there is no way for Matlab to recognize that beforehand. So it throws an error.
Thanks,but I still feel confused. I did not use the variable i just for repeating 8 times.so who runs in parallel? And I had another problems.
An UndefinedFunction error was thrown on the workers for 'configurePin'. This might be because the file containing 'configurePin' 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: Error using parallel_function>make_general_channel/channel_general (line 929) Undefined function 'configurePin' for input arguments of type 'double'.
I had no idea what it meant....
I think you want to use for instead of parfor. parfor is used for parallel computing and doesn't make much sense when generating figures.

Sign in to comment.

More Answers (1)

Really? The reason why I use the function of parfor is beacause I want to make it plot quickly. Now ,I made time and fequency domains respectively,but the sampling rate was slower than the LabView's. So,that's why I wanted to seek the faster way to replace the LabView.
Thanks for your answering,thank you very much, Marc.

Categories

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!