-1

I get an error when I run a Matlab code (attached), that continuosly check and opens two .txt files at a given time interval, (2 seconds) and depending on the result a value comparison between those two files, chose one or another and use it.... Those two files are constantly saved and updated by a Java script at the same time interval of 2 seconds. All the files are located in the same path.

The error I get is: "invalid file identifier. use fopen to generate a valid file identifier

Error in KinectDEMelevation_with_filecomparison2 (line 36) DEM1 = textscan(fid2,formatSpec,'HeaderLines',6,'Delimiter','\b'); "

The code is:

DEM = GRIDobj('kinectDEM0.txt');
clims = [-30 140];
imagesc(DEM,clims);
colormap(jet);               
hold on

while(1)
    tic
%     clear all
%     clf
%     load('MyColormaps','mycmap');

    %% Get kinectDEM0 data

    % Open 'kinectDEM0.txt'
    fid1 = fopen('kinectDEM0.txt', 'r+');
    % Read data in from the .txt file
    formatSpec = '%n';
    DEM0 = textscan(fid1,formatSpec,'HeaderLines',6,'Delimiter','\b');
    % Extract data from DEM0
    DEM0Data = DEM0{1,1}(200:100:287800,1);
    % Close 'kinectDEM0.txt'
    fclose(fid1);

    %% Get kinectDEM1 data

    % Open 'kinectDEM1.txt'
    fid2 = fopen('kinectDEM1.txt', 'r+');
    % Read data in from the .txt file
    formatSpec = '%n';
    DEM1 = textscan(fid2,formatSpec,'HeaderLines',6,'Delimiter','\b');
    % Extract data from DEM1
    DEM1Data = DEM1{1,1}(200:100:287800,1);
    % Close 'kinectDEM1.txt'
    fclose(fid2);

    %% Compare data, a logical array return 1 for true (points that has been changed), 0 for false


    test = eq(DEM0Data,DEM1Data);
    num = sum(test);                    % numbers of point in the scene that has been changed
    threshold = 2900;                   % after this threshold update the image

    if num > threshold
        DEM = GRIDobj('kinectDEM1.txt');
        clf
        clims = [-30 140];
        imagesc(DEM,clims);
        colormap(jet);                  
        hold on

    end

    T = toc;
    pause(2 - T);

end

How can I fix it?

Thanks

Geonik
  • 65
  • 7
  • 1
    don't use files as a interprocess-communication, especially since the access (read/write) is not synchronized in your case. – m.s. May 30 '15 at 12:47
  • I know it's a problem, but I don't know how to skip this problem, the only way to write those .txt files is using that java script. However the java script saves the files as a .tmp and only when they are completely written are renamed to .txt in order to make it available to the Matlab script only if they're completely written. – Geonik May 30 '15 at 17:07

1 Answers1

0

You should add some error checking to make sure the file:

  • exists
  • has been successfully opened

For the first, you can use exist:

r = exist('kinectDEM0.txt','file');
% will return 2 if file exists

For the second, you need to check fid is valid. If fopen cannot open the file, this will be -1. You may also use the second output for fopen, errmsg, for additional checking; errmsg should be an empty string if the file was opened successfully, otherwise it will return the system error message, such as "No such file or directory".

A simple example:

fid = -1
while fid == -1
    fid = fopen('myfile.txt'); % attempt to open until success
end
nkjt
  • 7,825
  • 9
  • 22
  • 28