Unable to delete or edit a file opened & closed with fopen() & fclose(). Error: "file is open in JxBrowser Chromium Native Process"

65 visualizzazioni (ultimi 30 giorni)
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);
end
while 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.

Risposta accettata

Kota Matsuo
Kota Matsuo il 13 Gen 2017
Modificato: Kota Matsuo il 13 Gen 2017
Never mind. I split the read and write parts to two separate functions, used a try-catch block with fclose('all') in both functions, and now everything works fine. The "dummy_file.txt" file can be edited, either manually or by other programs, with no problem, while the matlab code is continuously monitoring the content of the file. Hope it helps whoever is reading this!
write_to_dummy('dummy_file.txt', '0');
dummy = read_dummy('dummy_file.txt');
while dummy == '0'
pause(1)
dummy = read_dummy('dummy_file.txt')
%Wait until another program performs and finishes calculation and rewrites the "0" to "1"
end
function write_to_dummy(fileName, varargin)
try
fid = fopen(fileName,'w+t');
fprintf(fid,varargin{1});
fclose('all');
catch me
fclose('all');
disp('error while writing to the dummy_file.txt')
rethrow(me);
end
end
function dummy = read_dummy(fileName)
try
fid = fopen(fileName,'rt');
fseek(fid,0,-1); %go to beginning of the file
dummy = fgetl(fid);
fclose('all');
catch me
fclose('all');
disp('error while reading the dummy_file.txt')
rethrow(me);
end
end

Più risposte (1)

Walter Roberson
Walter Roberson il 9 Gen 2017

Community Treasure Hunt

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

Start Hunting!

Translated by