フィルターのクリア

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 Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by