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

In summary, the conversation discussed an issue with importing multiple .csv files into MATLAB using a loop. The error "The file extension '.' is not recognized" was encountered due to the presence of multiple periods in the file names. The solution involved using the 'HeaderLines' parameter in the readtable function and modifying the file names to remove the periods. A code example using table2array was provided to help with this issue.
  • #1
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
  • #2
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.
 
  • #3
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
 
  • #4
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.
 
  • #5
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:
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
 
Last edited:
  • Like
Likes member 428835
  • #6
Perfect, thanks a ton! PF for the win!
 

1. What is the best way to import multiple .csv files into a single dataset?

The most efficient way to import multiple .csv files into a single dataset is to use a programming language such as Python or R. These languages have built-in functions and libraries that allow you to easily read and combine multiple .csv files.

2. Can I import multiple .csv files with different column headers?

Yes, you can import multiple .csv files with different column headers. However, you will need to make sure that the column names are consistent across all files before combining them. This can be done using the "rename" function in programming languages like Python or R.

3. Is there a limit to the number of .csv files I can import at once?

The limit to the number of .csv files you can import at once will depend on the capabilities of your computer and the programming language you are using. However, it is generally recommended to import a manageable number of files at a time to avoid overwhelming the system.

4. Can I specify the order in which the .csv files are imported?

Yes, you can specify the order in which the .csv files are imported by using the "concat" function in programming languages like Python or R. This function allows you to specify the order in which the files should be combined.

5. Are there any potential issues I should be aware of when importing multiple .csv files?

One potential issue to be aware of when importing multiple .csv files is the presence of duplicate data. If the files have overlapping data, it is important to identify and remove any duplicates to avoid skewing your dataset. Additionally, make sure to check for any missing or incorrect data that may affect the accuracy of your results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
8K
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
7K
Back
Top