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 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
Réponses (1)
Stephen23
le 8 Sep 2020
Modifié(e) : Stephen23
le 8 Sep 2020
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 commentaires
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!