I'm trying to run MATLAB and another program simultaneously; make them perform calculations alternatively. The idea is to first make MATLAB calculate something, and after that's done, output "0" into a dummy text file. The other program starts its calculation when it reads the "0" in the dummy text file (while MATLAB waits doing nothing), and when that's done, it rewrites the "0" in the dummy text file to "1". MATLAB then resumes its calculation when it reads the "1" in the dummy text file, and rewrites the "1" to "0" when its done, and so on and so on…
See the below code.
try fid = fopen('dummy_file.txt','r+t'); fprintf(fid,'0'); fseek(fid,0,-1); %go to beginning of the file
value = fgetl(fid); fclose('all');catch me fclose('all'); disp('error handling dummy_file.txt') rethrow(me);endwhile value == '0' pause(10) %Wait until another program performs and finishes calculation and rewrites the "0" to "1"
end
The problem is, when I open and close the dummy text file in MATLAB, the text file becomes un-editable until I manually close the MATLAB program (matlab.exe). An inquiry of fopen('all') returns []. And still, I am unable to edit the text file (even manually) because of an error saying "The action cannot be completed because the file is open in JxBrowser Chromium Native Process".
I searched tremendously and found that someone asked the same question in 2008, but no real solution has been posted. https://www.mathworks.com/matlabcentral/newsreader/view_thread/170147
I tried using "fclose('all')", defining a class (explained here… http://stackoverflow.com/questions/8847866/how-can-i-close-files-that-are-left-open-after-an-error/8847870#8847870), using a try-catch block and close the file handles, and using the "onCleanup" function, but unfortunately nothing worked.
I'm completely stuck on this problem and would greatly appreciate your help.
Best Answer