Problem with parfor-loop. Unable to classify a variable in the body of the parfor-loop. Can someone help me?

72 views (last 30 days)
Hi everyone,
I'm Carlo and I'm using Matlab R2020a.
I'm trying to increase the speed of this for loop:
I explain a bit:
S is a table containing the datetimes in S.data.
AddSec is an array containing numbers. S and AddSec have the same number of elements.
What I have to do is to add to the i-th element in S.data the i-th element, as seconds, contained in the AddSec array.
For example, S.data(1) = 21-May-2020 00:00:17
AddSec(1) = 1
therefore: S.data(1) have to become 21-May-2020 00:00:18
The for loop requires too much time, since height(S) is more than 4 million.
for i=1:height(S)
S.data(i)= datestr(addtodate(datenum(S.data(i)),AddSec(i),'second'));
end
I would like to increase the efficiency of this loop and I tried to use the parfor as follow. As I'm new with Matlab, and it's the first time that I try to use the parfor loop, I ask you suggestions on how to solve my problem using the parfor:
Error: Unable to classify the variable 'S' in the body of the parfor-loop. For more information, see Parallel for Loops
in MATLAB, "Solve Variable Classification Issues in parfor-Loops".
The parfoor loop that I wrote is:
parfor i=1:height(S)
S.data(i)= datestr(addtodate(datenum(S.data(i)),AddSec(i),'second'));
end

Accepted Answer

Arthur Roué
Arthur Roué on 24 Jul 2020
Edited: Arthur Roué on 24 Jul 2020
You cannot create a structure in a parfor-loop using dot notation assignment. Here is a workaround
parfor i = 1:height(S)
Data(i)= datestr(addtodate(datenum(S.data(i)),AddSec(i),'second'));
end
S.data = Data;
I suggest you to read this article Troubleshoot Variables in parfor-Loops which is really good. There is a section for strcture arrays in parfor loops.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!