How can I import multiple .csv files into MATLAB using a loop?

  • Context: MATLAB 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    files Multiple
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 4K views
member 428835
Hi PF!

I'm trying to import multiple .csv files into MATLAB. Since the first row of the .csv is titles, I'm using the readtable function. This process works when I don't use a loop, but when I try to loop through several files I get the error "Error using readtable (line 198) The file extension '.' is not recognized."

The files are called RChan0.0.csv, RChan0.1.csv, RChan0.2.csv...

What I have so far is

Code:
N         = 2;
intfce  = zeros(206,3,N);

for ii = 1:N
    intfce(:,:,ii) = readtable('/Users/Josh/Desktop/RChan/RChanTheta71/RChan0.',num2str(ii-1),'.csv');
end

Any ideas?
 
Physics news on Phys.org
I think it might be getting confused by the multiple '.'s in the file names and getting an invalid extension. Do you have the same problem if the file names are like RChan0p0.csv, RChan0p1.csv, RChan0p2.csv... ? That is how I used to represent decimal points in file names. I don't remember what exactly forced me to do that.
 
Here's the page for readtable. There must be an option to ignore the first line.

https://www.mathworks.com/help/matlab/ref/readtable.html
'HeaderLines' — Lines to skip
positive integer

Lines to skip at beginning of the file, specified as the comma-separated pair consisting of 'HeaderLines' and a positive integer. If unspecified, readtable automatically detects the number of lines to skip.
Data Types: single | double
 
FactChecker said:
I think it might be getting confused by the multiple '.'s in the file names and getting an invalid extension. Do you have the same problem if the file names are like RChan0p0.csv, RChan0p1.csv, RChan0p2.csv... ? That is how I used to represent decimal points in file names. I don't remember what exactly forced me to do that.
Hmmmm so I tried replacing the first . with a p and nothing. I then went into the file and deleted the first . making the first few files RChan00.csv, RChan01.csv but I receive the error:

Error using readtable (line 198)
Invalid parameter name: 0.

Jedi, give me a moment to try and read what you're saying.
 
There is a problem storing a table with a mixture of column titles and data. I broke the lines of your code into smaller parts and added a strcat and table2array and used it on a small example. This seemed to work. I hope you can modify it for your larger problem:
[CODE lang="matlab" title="Works on a small, 2-file,2-row,2-col example"]N = 2;
numRows = 2;
numCols = 2;
intfce = zeros(numRows,numCols,N);

for ii = 1:N
fileName = strcat('C:\Users\hollimb\Desktop\installations\home_bin\MATLAB\RChan0p',num2str(ii-1),'.csv');
data = readtable(fileName);
intfce(:,:,ii) = table2array(data);
end[/CODE]
 
Last edited:
  • Like
Likes   Reactions: member 428835