Cause of invalid file identifier (no such file or directory)?

27 views (last 30 days)
The following code (to read and open files) was written and tested with MATLAB R2012a on a windows7 operating system:
S = 'runoff.txt'; % Name of the file
D = '/gpfs/gpfs1/scratch/c77777/results/model_standalone_xc';
d = dir(fullfile(D,'Riffelsee*')); % directory list
%fmtS = ['%{dd.MM.yyyy-HH:mm}D' repmat('%*f',1,5) '%f']; % Format
fmtS = ['%s',repmat('%f',1,6)]; % Format
opt = {'HeaderLines',1,'CollectOutput',true}; % header
L=length(d); % number of subfolders
YC=zeros(L,3); % preallocate
for k = 1:L % Iteration over sufolders
[fid,message] = fopen(fullfile(D,d(k).name,S),'rt'); % open files
if fid < 0
disp(message);
Z = [];
else
Z=textscan(fid,fmtS,opt{:}); % read simulated values
end
fclose(fid); % close
dtS=Z{:,1}; % timestamp simulated (datetime)
Qs=Z{:,2}(:,6); ....
Opening and reading the files worked without errors.
Now the m-file should run on a cluster environment (Linux). A standalone version was created for this purpose.
If I submit this as a job the following error message appears:
_No such file or directory
Error using fclose
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in ... (line fclose(fid))
MATLAB:FileIO:InvalidFid_
In the specified directory all files are included and the path appears to me as correct.
What could be the problem?

Accepted Answer

Jan
Jan on 27 Jul 2017
Edited: Jan on 27 Jul 2017
Obviously the path is not correct. You can check this easily:
File = fullfile(D, d(k).name, S);
[fid, message] = fopen(File, 'rt'); % open files
if fid < 0
fprintf('Not found: %s\n%s\n', File, message);
Z = [];
else
Z=textscan(fid,fmtS,opt{:}); % read simulated values
fclose(fid); % <-- move inside the IF branch
end
You are searching for the pattern
/gpfs/gpfs1/scratch/c77777/results/model_standalone_xc/Riffelsee*
In all these folders (or files?!) your search for the files:
/gpfs/gpfs1/scratch/c77777/results/model_standalone_xc/Riffelsee*/runoff.txt
and the error message tells you, that one of these files does not exist. I'm sure Matlab has detected this carefully, so trust the message.
Note: Your code will fail, if you try to access dtS=Z{:,1}, if you have set Z=[].
  1 Comment
Glazio
Glazio on 1 Aug 2017
Edited: Glazio on 1 Aug 2017
@Jan Simon: Thanks for your answer. There are no files missing, but some files have different names.

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!