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