Can a system() Command be used Inside a Function Called from Inside a parfor Loop?
1 view (last 30 days)
Show older comments
Is the following code allowed:
result = zeros(100,1);
parfor ii = 1:100
% do something
result(ii) = myfun(resultfromdoingsomething)
end
function result = myfun(inp)
% do something and write a file based on inp
status = system(command); % command executes an executable that reads in the file that was written and produces an output file
result = % read in the file produced by the executable and grab a value to return
% delete the files
end
Even if allowed, are there any risks to be concerned about?
Would also like to know if there are any differences between 2024a and 2019b in this regard.
0 Comments
Accepted Answer
Epsilon
on 13 Sep 2024
Hi Paul,
A 'system' command inside a function called from within ‘parfor’ seems to be working fine.
Code:
function myFunction(index)
command = sprintf('echo Processing index %d', index);
[~, cmdout] = system(command);
disp(['Output for index ', num2str(index), ': ', cmdout]);
end
parfor i = 1:100
myFunction(i);
end
One of the issues can be when multiple iterations try to access shared resources and can create conflicts.
Both the ‘parfor’ and ‘system’ are supported by the releases R2024a and R2019b so it should work fine.
Links to the Documentations:
system(R2024a): https://www.mathworks.com/help/matlab/ref/system.html
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!