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?
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!