Fail to load files in Matlab with an error using fgets (Invalid file identifier)

by Coco_viva   Last Updated September 21, 2019 08:26 AM - source

I want to parse the text file with my m-file. The text file stores the annotations of DICOM images in the same folder. However, I am confronted with the fgets error and unable to move on.

Since my friend could run this m-file on his laptop, I am not sure whether my problem is due to the online Matlab, but not the desktop version, which I am using. Thanks in advance for any advice!

My m-file contents:

function ROIs = loadROIfiles(ROIfileName)
% Loads all the ROIs stored in the *.txt file from the specified location.
% The ROIs are returned as a structure array as:
% ROIs(i).spacing_x (double)
% ROIs(i).spacing_y (double)
% ROIs(i).label (string)
% ROIs(i).slice_number (double)
% ROIs(i).xValues (vector of double)
% ROIs(i).yValues (vector of double)
% ROIs = loadROIfiles(ROIfileName)
% @author University & University Hospitals of Geneva (HUG) MedGIFT Group
%         Adrien Depeursinge [email protected]
%      Copyright (C) 2012, MedGIFT group at University Hospitals of Geneva
%      This program is free software; you can redistribute it and/or modify
%      it under the terms of the GNU General Public License as published by
%      the Free Software Foundation; either version 2 of the License, or
%      (at your option) any later version.
%      This program is distributed in the hope that it will be useful,
%      but WITHOUT ANY WARRANTY; without even the implied warranty of
%      GNU General Public License for more details.
%      You should have received a copy of the GNU General Public License
%      along with this program; if not, write to the Free Software
%      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

fid = fopen(ROIfileName, 'r');
while 1
    tline = fgetl(fid);
    if ~ischar(tline), break, end

    if (isempty(findstr(tline, 'SpacingX:')) == false)
        spacing_x = str2double(strrep(tline, 'SpacingX: ', ''));
    elseif (isempty(findstr(tline, 'SpacingY:')) == false)
        spacing_y = str2double(strrep(tline, 'SpacingY: ', ''));
    elseif (isempty(findstr(tline, 'label:')) == false)
        label = strrep(tline, 'label: ', '');
    elseif (isempty(findstr(tline, 'slice_number:')) == false)
        slice_number = str2double(strrep(tline, 'slice_number: ', ''));
    elseif (isempty(findstr(tline, 'nb_points_on_contour:')) == false)
        nb_points_on_contour = str2double(strrep(tline, 'nb_points_on_contour: ', ''));
        xValues = [];
        yValues = [];
        for j=1:nb_points_on_contour
            tline = fgetl(fid);
            [xValStr, yValStr] = strtok(tline, ' ');
            xVal = str2double(xValStr);
            yVal = str2double(yValStr);
            xValues = [xValues xVal];
            yValues = [yValues yVal];
        % store the ROI into the struct array "ROIs"
        ROIs(i).spacing_x = spacing_x;
        ROIs(i).spacing_y = spacing_y;
        ROIs(i).label = label;
        ROIs(i).slice_number = slice_number;
        ROIs(i).xValues = xValues;
        ROIs(i).yValues = yValues;



Code in command window:

ROIs = loadROIfiles('CT-INSPIRIUM-6817.txt')


Error using fgets
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in fgetl (line 32)
[tline,lt] = fgets(fid);

Error in loadROIfiles (line 38)
    tline = fgetl(fid);

Related Questions

How do I identify multiple digits in random images?

Updated December 10, 2017 12:26 PM