0

I have 2 instances of Matlab running. While the first is writing data to a .txt file, the other is reading that data.

Is there a way to verify that the .txt file is being accessed and accordingly throw an exception/error?

I found that the second Matlab instance reads the data anyways but generates an error such as Horzcat etc while that .txt file was being updated as well.

fName = 'Test.txt' ;

% Matlab Instance1
mat = 1 + (2-1)*randn(100000,5)   ;      mat = mat.' ;

[fid, fMsg] = fopen(fName, 'at') ;
if fid~=-1,  fprintf(fid, '%.10f\t%.10f\t%.10f\t%.10f\t%.10f\r\n', mat(:)) ; end
fclose(fid);

    % Matlab Instance2
    fid = fopen(fName);
    C = textscan(fid, '%f %f %f %f %f', 'Delimiter', '\t');
    C=cell2mat(C);
    fclose(fid);
Amro
  • 123,847
  • 25
  • 243
  • 454
Maddy
  • 2,520
  • 14
  • 44
  • 64
  • 1
    Read this topic http://stackoverflow.com/questions/3451343/atomically-creating-a-file-lock-in-matlab-file-mutex – Cheery Feb 29 '12 at 22:55

1 Answers1

0

On the writing instance create a file called 'busyWriting.bla' before opening the file for writing, delete this file after you are done writing. And on the reading instance enclose everything with the clause if(~exist('busyWriting.bla','file')) ... end

zamazalotta
  • 423
  • 1
  • 6
  • 11