Cause of invalid file identifier (no such file or directory)?
조회 수: 26 (최근 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?
댓글 수: 0
채택된 답변
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=[].
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File 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!