How to terminate a matlab script called from python?
2 views (last 30 days)
Show older comments
I am running a python script which calls a certain number of threads which execute a certain code and a matlab thread in which I make subprocess call to open matlab and run a matlab script. Now I need to exit the matlab script if all the other threads in the python script has finished executing.
As of now I am using files to solve this. I have a file which contains '1' value. The matlab script keeps reading this file and is running until value changes to '0'. I am writing '0' to the file once all other threads are executed in the pyhton script.
def main(numOfThreads):
start_time = time.time()
launch_matlab_process()
launch_child_processes(numOfThreads)
join_child_processes(numOfThreads)
print_matlab_contents()
#To close the matlab thread when all other threads are finished
filename = os.path.join(CURRENT_DIR, "finished.txt")
f = open(filename, 'w') # script path
f.write("0")
f.close()
#Matlab Script
CURRENT_SCRIPT_DIRECTORY = mfilename('fullpath');
[filepath,name,ext] = fileparts(CURRENT_SCRIPT_DIRECTORY);
file = fullfile(filepath,'file.txt');
file1 = fullfile(filepath, 'finished.txt');
a = 1;
fileID = fopen(file, 'w');
fprintf(fileID,'Matlab File\n');
fileID1 = fopen(file1, 'r+');
%Keep checking file for value
while strcmp(fileread(file1), '1')
fprintf(fileID,'Line Number: %d\n', a);
a = a + 1;
pause(1);
end
fprintf(fileID1, '%c', '1'); %Make file 1 again
fclose(fileID);
fclose(fileID1);
So is there a better way
So is there a better way of doing this?
0 Comments
Answers (0)
See Also
Categories
Find more on Call Python from MATLAB 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!