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

27 次查看(过去 30 天)
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?

采纳的回答

Jan
Jan 2017-7-27
编辑:Jan 2017-7-27
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 个评论
Glazio
Glazio 2017-8-1
编辑:Glazio 2017-8-1
@Jan Simon: Thanks for your answer. There are no files missing, but some files have different names.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by