Error using fseek - while running the matlab code. Input to the program looks fine.

23 views (last 30 days)
Error using fseek
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in dicom_getFileDetails>getFileLength (line 33)
fseek(fid, 0, 'eof');
Error in dicom_getFileDetails (line 21)
details.bytes = getFileLength(fid);
Error in dicominfo (line 51)
fileDetails = dicom_getFileDetails(filename);
Error in Generate_RS_Object_DICOM (line 3)
rr = dicominfo(stfile,'UseVRHeuristic',false); % return structure rr
Error in Structure_To_Binary_Mask (line 5)
RS = Generate_RS_Object_DICOM(RSFile);
Error in SIFT_RUN_SET_Thorax_PMB (line 32)
[img1, imgB1L,Pos1,Res1,Dim1,~,~] = Structure_To_Binary_Mask(CTDir1,RSFile1,'LUNL');
  4 Comments
Shiba Kuanar
Shiba Kuanar on 6 May 2019
I am passing my absolute path for the files - through a variable. Please guide me if any otherthings i need to try.
Adam Danz
Adam Danz on 7 May 2019
I'm not familiar with some of the functions listed in your error message so I can't give good advice on the solution. fseek() requires a file identifier that is created by fopen(). Instead, you mentioned that you're providing a path to the file. That seems to be causing the error. So at some point you need to produce the fid as I showed in my answer below.
If this is confusing or you need more feedback, please continue the discussion.

Sign in to comment.

Answers (2)

Guillaume
Guillaume on 7 May 2019
Edited: Guillaume on 7 May 2019
Which version of matlab are you using (there's a field on the right at the top of this web page for you to fill in)? From the error message it's not the latest, as the line in dicominfo that produces the error is now line 55. However, it does appear that the subfunctions dicom_getFileDetails and dicom_getFileDetails>getFileLength are identical to yours in 2019a.
Typically, the error you see is caused by a failed fopen but dicom_getFileDetails does check that the file opened successfully before calling fseek, so the fseek should always succeed unless your file system is somehow corrupted or your computer somehow lose access to the file after opening it. (e.g. file on usb drive that is suddenly disconnected). Or possibly your anti-virus interferes.
To help with diagnosis, can you type
dbstop if error
at the command line, run your code and when it breaks into the debugger because of the error, tell us the value of fid.
----- addendum ----
Actually, looking more closely at dicom_getFileDetails, there's a potential for a failed fopen to not be detected. I'll be submitting a bug report to Mathworks.
If the exact file name you supply is not found, the codes tries to find a matching file by adding various extensions (.dcm, .dic, .dicom, .img). If the file exists, the code opens it without checking that the fopen is indeed succesfull. So if you don't specify the extension but the file with extension exists but cannot be opened then you'll see the error you saw. Possible reason for a file to exist but not being able to be opened is that you've got read permission on the directory but not the file.
What is the exact filename you're passing to the function?

Adam Danz
Adam Danz on 6 May 2019
I don't know what inputs are expected for your custom function but the error is happening because fseek() expects a file identifier which is created by fopen().
fid = fopen('yourfile.ext');
% and then later,
fclose(fid);
  3 Comments
Adam Danz
Adam Danz on 7 May 2019
I'm assuming the problem starts in Structure_To_Binary_Mask() where the second input (RSFile1) seems to point to a file that is passed to Generate_RS_Object_DICOM() and then perhaps on to dicominfo(). Those first two functions are custom.
My guess is that those custom functions rely on a string that identifies a file and that string is passed forward and eventually causes in error where a file identifier is expected.
Shiba Kuanar
Shiba Kuanar on 8 May 2019
Thank You all. I am able to move forward. Issue was i was passing multiple files on the parameter.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!