필터 지우기
필터 지우기

Invalid file identifier. Use fopen to generate a valid file identifier.

조회 수: 3 (최근 30일)
Mainul Hoque
Mainul Hoque 2020년 9월 8일
편집: Stephen23 2020년 9월 8일
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
Mainul Hoque
Mainul Hoque 2020년 9월 8일
I have tried to attached .dat file but unfortunately I cann't. So I change the type of data file to .txt. Thanks

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
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.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by