how to solve the fopen error?

24 ビュー (過去 30 日間)
Lilya
Lilya 2017 年 8 月 17 日
コメント済み: Lilya 2017 年 8 月 17 日
Hi all,
I've done the below code to read .txt files and extracting columns from each one. the error appears just from the second to the rest of files. Could anyone help me with this?
thank you
Error using fgets
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in RUV_readFile (line 21)
line = fgets(file1);
clc
clear all
close all
%read file line by line
%loop over the files from list
%open each file and loop over the lines,
files_str = fileread('foldername/files.txt');
list = splitlines(files_str);
for k=1:length(list)
filename = list{k}; %index cell array of filenames using k
disp(filename);
file1 = fopen(strcat('foldername',filename));
filemat = [];
line = fgets(file1);
while ischar(line)
if isempty(strfind(line,'%')) %if line doesn't contains %
vec = str2num(line); %convert line string to vector
filemat = [filemat;vec]; %fill matric from vectors in file
end
% disp(line)
line = fgets(file1);
end
fclose(file1);
cols = filemat(:,1:4);
save(strcat('output/',filename), 'cols', '-ascii')
end
  3 件のコメント
Stephen23
Stephen23 2017 年 8 月 17 日
編集済み: Stephen23 2017 年 8 月 17 日
@Guillaume: thank you for the detailed explanation. It was remiss of me to not explain as much as you did.
@Lina Eyouni: you should read the MATLAB documentation, which shows just how simple this should be:
Lilya
Lilya 2017 年 8 月 17 日
Thanks Guillaume I am applying this now but without the apostrophes, it gives me an error. but I am working now with reading it from the links above. the other thing that I would like to clarify is the folder name. the name above is just to show the case because mine here is too long. I apologize.
thank you once again

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2017 年 8 月 17 日
編集済み: Stephen23 2017 年 8 月 17 日
  1. Use fullfile instead of the rather inappropriate strcat.
  2. Return the second output of fopen and check this using assert.
[file1,msg] = fopen(fullfile('foldername',filename));
assert(file1>=3,msg)
  8 件のコメント
Stephen23
Stephen23 2017 年 8 月 17 日
編集済み: Stephen23 2017 年 8 月 17 日
Then you have no problem. If the first output is >=3 then fopen successfully opened the file.
Note that you should replace ischar:
while ischar(line)
with feof:
while ~feof(file1)
Also note that this:
line = fgets(file1);
while ischar(line)
if isempty(strfind(line,'%')) %if line doesn't contains %
vec = str2num(line); %convert line string to vector
filemat = [filemat;vec]; %fill matric from vectors in file
end
% disp(line)
line = fgets(file1);
end
is a very inefficient way to read numeric data into MATLAB. You have not described what format the files have, but you should really consider using the inbuilt functions for reading numeric data:
Lilya
Lilya 2017 年 8 月 17 日
Ok. I will try with this now. Thank you so much for your response and help

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by