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
Click For Summary

Discussion Overview

The discussion focuses on importing multiple .csv files into MATLAB using a loop, specifically addressing issues encountered with the readtable function when handling file names with decimal points. Participants explore potential solutions and workarounds for the errors experienced during the import process.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an error encountered when using readtable in a loop, suggesting that the function may be confused by multiple periods in the file names.
  • Another participant proposes changing the file naming convention to avoid multiple periods, questioning whether this would resolve the issue.
  • A third participant references the readtable documentation, suggesting the use of the 'HeaderLines' option to skip the first line of the .csv files.
  • One participant attempts to modify the file names and encounters a different error related to invalid parameter names, indicating ongoing challenges with the import process.
  • Another participant shares a modified approach using strcat and table2array, which they found effective for their example, suggesting it could be adapted for larger datasets.

Areas of Agreement / Disagreement

Participants express differing views on the cause of the error and potential solutions, with no consensus reached on the best approach to resolve the issue.

Contextual Notes

Limitations include the potential for unresolved issues related to file naming conventions and the specific structure of the .csv files being imported. The discussion does not clarify whether all proposed solutions would universally resolve the initial error.

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
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
3K
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
3
Views
5K