Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab Transparency violation error while the code is correct

I am using the Matlab parallel toolbox to do parallel computing. I've doubled check the variable definition of the code, and it follows the requirements of parfor command. However, Matlab still told me that "Transparency Violation Error," could you help me figure out this issue?

Here is the source code.

load Wind80.mat
Wspeed_80 = Wind80;
TotalLoc = 4000;
Nloc = 5; % no of loc of each run
maxrun = ceil(TotalLoc/Nloc);
StrInfo.Pstr = 1; 
StrInfo.Tstr = 0:4:72;
tic
parfor run = 1:maxrun
   WT1 = [];
   WT2 = [];
   [WT1, WT2] = CompWTGenFun(run, Nloc, TotalLoc, StrInfo);
   filenm = ['ResultPara' num2str(StrInfo.Pstr) 'Run' num2str(run) '.mat' ];
   save(filenm, 'WT1', 'WT2', '-mat');
   clear WT1 WT2
end
toc
like image 971
AIRicky Avatar asked Feb 14 '26 08:02

AIRicky


1 Answers

You cannot use the save function within a parfor loop, as this will result in a transparency violation. Transparency violations occur when a function needs to look into (or modify) its calling workspace. Unfortunately, save does this - in your code, you give the save command the names of the variables you want to save, and the save implementation attempts to extract the values from its calling workspace (i.e. the body of the parfor loop).

The workaround is to hide the call to save inside a separate function, in other words, you need something like this:

parfor ...
    mySave(filenm, WT1, WT2);
end
# ...
function mySave(filenm, WT1, WT2)
    save(filenm, 'WT1', 'WT2', '-mat');
end

This works because the transparency constraint only applies to the code directly present in the body of the parfor loop.

As mentioned in the comments, the clear command you have is not necessary, and would again cause a transparency violation.

like image 112
Edric Avatar answered Feb 16 '26 14:02

Edric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!