Can a system() Command be used Inside a Function Called from Inside a parfor Loop?

3 views (last 30 days)
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.

Accepted Answer

Epsilon
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
Output for index 100: Processing index 100 Output for index 99: Processing index 99 Output for index 98: Processing index 98 Output for index 97: Processing index 97 Output for index 96: Processing index 96 Output for index 95: Processing index 95 Output for index 94: Processing index 94 Output for index 93: Processing index 93 Output for index 92: Processing index 92 Output for index 91: Processing index 91 Output for index 90: Processing index 90 Output for index 89: Processing index 89 Output for index 88: Processing index 88 Output for index 87: Processing index 87 Output for index 86: Processing index 86 Output for index 85: Processing index 85 Output for index 84: Processing index 84 Output for index 83: Processing index 83 Output for index 82: Processing index 82 Output for index 81: Processing index 81 Output for index 80: Processing index 80 Output for index 79: Processing index 79 Output for index 78: Processing index 78 Output for index 77: Processing index 77 Output for index 76: Processing index 76 Output for index 75: Processing index 75 Output for index 74: Processing index 74 Output for index 73: Processing index 73 Output for index 72: Processing index 72 Output for index 71: Processing index 71 Output for index 70: Processing index 70 Output for index 69: Processing index 69 Output for index 68: Processing index 68 Output for index 67: Processing index 67 Output for index 66: Processing index 66 Output for index 65: Processing index 65 Output for index 64: Processing index 64 Output for index 63: Processing index 63 Output for index 62: Processing index 62 Output for index 61: Processing index 61 Output for index 60: Processing index 60 Output for index 59: Processing index 59 Output for index 58: Processing index 58 Output for index 57: Processing index 57 Output for index 56: Processing index 56 Output for index 55: Processing index 55 Output for index 54: Processing index 54 Output for index 53: Processing index 53 Output for index 52: Processing index 52 Output for index 51: Processing index 51 Output for index 50: Processing index 50 Output for index 49: Processing index 49 Output for index 48: Processing index 48 Output for index 47: Processing index 47 Output for index 46: Processing index 46 Output for index 45: Processing index 45 Output for index 44: Processing index 44 Output for index 43: Processing index 43 Output for index 42: Processing index 42 Output for index 41: Processing index 41 Output for index 40: Processing index 40 Output for index 39: Processing index 39 Output for index 38: Processing index 38 Output for index 37: Processing index 37 Output for index 36: Processing index 36 Output for index 35: Processing index 35 Output for index 34: Processing index 34 Output for index 33: Processing index 33 Output for index 32: Processing index 32 Output for index 31: Processing index 31 Output for index 30: Processing index 30 Output for index 29: Processing index 29 Output for index 28: Processing index 28 Output for index 27: Processing index 27 Output for index 26: Processing index 26 Output for index 25: Processing index 25 Output for index 24: Processing index 24 Output for index 23: Processing index 23 Output for index 22: Processing index 22 Output for index 21: Processing index 21 Output for index 20: Processing index 20 Output for index 19: Processing index 19 Output for index 18: Processing index 18 Output for index 17: Processing index 17 Output for index 16: Processing index 16 Output for index 15: Processing index 15 Output for index 14: Processing index 14 Output for index 13: Processing index 13 Output for index 12: Processing index 12 Output for index 11: Processing index 11 Output for index 10: Processing index 10 Output for index 9: Processing index 9 Output for index 8: Processing index 8 Output for index 7: Processing index 7 Output for index 6: Processing index 6 Output for index 5: Processing index 5 Output for index 4: Processing index 4 Output for index 3: Processing index 3 Output for index 2: Processing index 2 Output for index 1: Processing index 1
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:

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!