Error while reading the .csv file

61 visualizaciones (últimos 30 días)
Kumaresh Kumaresh
Kumaresh Kumaresh el 28 de Nov. de 2024 a las 7:42
Comentada: Walter Roberson el 1 de Dic. de 2024 a las 4:49
While reading the .txt files, the below code runs good. But when I read .csv files, I end up with the following error.
Error using fopen
File identifier must be an integer-valued scalar of type double.
Error in check (line 12)
fid = fopen(fn, 'r');
Can someone help me to resolve this part.
clear
clc
%f=dir('*.txt'); % finding all txt files
f=dir('*.csv'); % finding all csv files
n_snapshots=size(f,1); % finding number of snapshots
for i=1:n_snapshots
%fn=f(i).name; % for .txt file
fn = readmatrix(f(i).name); % for .csv file
fid = fopen(fn, 'r'); % reading files
% scaning files into 4 columns - gives in rows
data=fscanf(fid, '%f %f %f %f' , [4, inf]);
% exporting columns
x=data(4,:);
y=data(3,:);
v(i,:)=data(1,:);
u(i,:)=data(2,:);
fclose('all');
end
Thank you

Respuestas (1)

Stephen23
Stephen23 el 28 de Nov. de 2024 a las 8:07
Editada: Stephen23 el 28 de Nov. de 2024 a las 8:17
Explanation: As its documentation makes clear, the first input to FOPEN must be the fiename. However, instead of calling it with a filename you call it with some matrix of data that you imported using READMATRIX. That will not work.
Solution: replace this
%fn=f(i).name;
fn = readmatrix(f(i).name);
fid = fopen(fn, 'r');
with
fid = fopen(f(i).name, 'r');
You should also replace
fclose('all');
with
fclose(fid);
But why are you calling FOPEN & FSCAN & FCLOSE when you could simply call READMATRIX ?
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.csv'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
M = readmatrix(F, 'FileType','text');
...
end
CSV files are text files. There is no reason why you cannot use READMATRIX to import both .CSV and .TXT files. You should use READMATRIX unless there is a really good reason why you cannot use it.
  9 comentarios
Kumaresh Kumaresh
Kumaresh Kumaresh el 1 de Dic. de 2024 a las 2:54
Thank you.
Is there any other way you can recommend to resolve this issue in current MATLAB version ?
Walter Roberson
Walter Roberson el 1 de Dic. de 2024 a las 4:49
R2019a to present: recommend you use readmatrix()
R2013b to R2018b: recommend you use readtable() followed by array2table()
Older but not super-super-super old versions of MATLAB: use fileread() on the file. Examine the character vector you get back to figure out how many lines of headers there are. Use textscan() on the character vector with 'HeaderLines' option telling it how many header lines to skip.
Super-super-super but not Super-super-super-super old versions of MATLAB: fopen() and use fgetl() to fetch lines and examine them, eventually finding the first numeric line. sscanf() to get the numeric content of that line. fscanf() the rest of the file to get the remaining numeric lines.
MATLAB 1.x and MATLAB 2.x: I don't know, those releases did not even have character vectors; I would need access to terribly old documentation to figure it out.
I simplified the lineage here. There were some versions in there that supported textread() before textscan() existed.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by