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

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    files Multiple
Click For Summary
The discussion centers on importing multiple .csv files into MATLAB using the readtable function. The user encounters an error related to file extensions when attempting to loop through files named RChan0.0.csv, RChan0.1.csv, etc. The issue is suspected to stem from the multiple periods in the filenames, leading to an invalid extension error. Suggestions include changing the file naming convention to avoid multiple periods, such as using 'RChan0p0.csv' instead. The user also explores modifying the code to break it into smaller parts and utilize strcat and table2array for better handling of the data. Ultimately, a working solution is found by adjusting the file naming and successfully importing the data into MATLAB.
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 member 428835
Perfect, thanks a ton! PF for the win!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K