Consider two MATLAB m files named profile.m and try_parfor.m as follows.
% profile.m
function profile()
olddir = pwd();
cd(tempdir);
fprintf('\nCurrent directory: %s\n', pwd());
func = @(x) fun(x, 0);
addpath(fullfile(fileparts(mfilename('fullpath')), 'try_parfor'));
try_parfor(func);
cd(olddir);
fprintf('\nCurrent directory: %s\n', pwd());
fprintf('\nSuccess!\n');
end
function y = fun(x, z)
y = x + z;
end
% try_parfor.m
function try_parfor(func)
fprintf('\nEntering try_parfor\n');
fprintf('\nCurrent directory: %s\n', pwd());
parfor i = 1:2
fprintf("\n%d\n", feval(func, i));
end
fprintf('\nExiting try_parfor\n');
end
If you have MATLAB R2023a--R2025a with the Parallel Computing Toolbox installed, try the following.
- Place
profile.min a directory namedtest. - Place
try_parfor.min thetry_parforsubdirectory oftest. - Open MATLAB and change the current directory to
test. - Run the following command in the MATLAB command window:
profile
It should fail with an error like the following.
Analyzing and transferring files to the workers ...done.
{Error using try_parfor (line 6)
The source code
(/home/runner/work/test_matlab/test_matlab/test_parfor/try_parfor/try_parfor.m)
for the parfor-loop that is trying to execute on the worker could not be found.
Error in profile (line 10)
try_parfor(func);
^^^^^^^^^^^^^^^^^
Caused by:
Unrecognized function or variable 'fun'.
Worker unable to find file.
Unrecognized function or variable 'fun'.
}
exit status 1
Question: Is this failure expected or a bug?
I understand that the failure must depend on the fact that MATLAB has a built-in function named "profile", which is shadowed by "profile.m". Needless to say, this is bad practice, but it is not clear to me why it should lead to such a failure.
As a comparison, rename profile.m to test.m and revise its function name accordingly as follows. Then the MATLAB command
test
will finish successfully.
% test.m
function test()
olddir = pwd();
cd(tempdir);
fprintf('\nCurrent directory: %s\n', pwd());
func = @(x) fun(x, 0);
addpath(fullfile(fileparts(mfilename('fullpath')), 'try_parfor'));
try_parfor(func);
cd(olddir);
fprintf('\nCurrent directory: %s\n', pwd());
fprintf('\nSuccess!\n');
end
function y = fun(x, z)
y = x + z;
end
See https://github.com/zequipe/test_matlab/tree/master/test_parfor for a reproducer of the issue described above, and https://github.com/zequipe/test_matlab/actions/workflows/test_parfor.yml for a GitHub Actions workflow reproducing it. See https://www.mathworks.com/matlabcentral/answers/2180029-an-issue-with-parfor-in-matlab-r2023a-r2025a for the same question on MATLAB Answers.
Update (made after the very nice and insightful answer & comments by @Edric): It is noteworthy that, as far as I can test, the failure does not occur on MATLAB R2021a—R2022b, as illustrated by my experiment using GitHub actions. However, Edric observes the failure on R2021b. I don’t have a physical machine with MATLAB R2021a—R2022b, and I cannot confirm whether this difference is due to something related to the setup of MATLAB on GitHub Actions. It would be nice if others could conduct a test if machines are available.