I have tried to attached .dat file but unfortunately I cann't. So I change the type of data file to .txt. Thanks
Invalid file identifier. Use fopen to generate a valid file identifier.
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi
I am using the following code to read a file and delete the three headres and footers lines from the dat. file. But unfortunately it shows the following error. Can anybody helpme to this issue please.
Error: " Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier."
clear;clc;close all
vec_dir='Z:\Flotation_chamber\8.0Volts\';
vectors=dir([vec_dir '*.dat']);
num_files=size(vectors,1);
for img_no=1:num_files
disp(['img :' num2str(img_no)]);
vector_nm=vectors(img_no).name;
% phase 1 : read the ith dat file
fid = fopen(vector_nm,'r');
TextDat = textscan(fid,'%s','delimiter','\n');
fclose(fid);
% phase 2 : just take from the 4th line (3 headers) until the end-3
% (3 footers)
NewTextDat = TextDat{1}(3+1:end-3);
% phase 3 : rewrite the file with the new data
fid = fopen(vector_nm,'wt');
fprintf(fid,'%s\n',NewTextDat{:});
fclose(fid);
end
답변 (1개)
Stephen23
2020년 9월 8일
편집: Stephen23
2020년 9월 8일
You need to use the directory information in two locations: both with dir and with fopen, e.g.:
vec_dir = 'Z:\Flotation_chamber\8.0Volts\';
vectors = dir(fullfile(vec_dir,'*.dat'));
...
vector_nm = fullfile(vec_dir,vectors(img_no).name);
As the fopen documentation states "If the file is not in the current folder, filename must include a full or a relative path." You did not provide the path to fopen, so it only looks in the current directory.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!