MATLAB Reading all .csv files with different names

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    files Reading
Click For Summary
To read all .csv files in a folder using MATLAB, the initial attempt to use a wildcard in the readtable function results in an error. Instead, it is recommended to use the dir function to list all .csv files in the specified directory. This approach allows for the retrieval of file attributes and facilitates loading each file individually using a loop. The discussion emphasizes the importance of understanding the underlying operating system, particularly UNIX or Linux, as it enhances control over file handling and operations. The user expresses gratitude for the guidance received and mentions efforts to improve their Linux skills through simple shell scripting.
member 428835
Hi PF!

I am trying to read all the .csv files in a folder. When I execute
Code:
OFdata  = readtable('/home/josh/Documents/NASA/PSI_DATA/Double_Drain/ICF1-9/OpenFOAM_DATA/*.csv');
I get a general error. However, replacing the asterisk with the file name, it load no issue.

I've seen online how to read several .csv files with only a numeric difference in name (for loops), but is there a way to read all .csv files with different names?
 
Last edited by a moderator:
Physics news on Phys.org
Code:
list  = dir('/home/josh/Documents/NASA/PSI_DATA/Double_Drain/ICF1-9/OpenFOAM_DATA/*.csv');
This should give you a list of all the csv files. You can then use a loop to load them one by one.
 
  • Like
Likes member 428835, jim mcnamara and jedishrfu
dir() calls the Linux OS - which globs the filenames it can find with the pattern: "*.csv". Then dir() executes stat() to get attributes -- on each file it finds. Like filetimes, file ownership, etc. - you can ask it do lots of tricks.

You can see the the one call python asks Linux to make for mulitiple objects. when you read the man page for glob. It works on filesystems and file, not what are categorized as python __dict__ objects.

From bash -- dir() is a window-ism for ls:
Code:
man glob
Code:
man ls

Most of the Linux OS is written in the C language, so part of the glob page shows how call it in your own C program. And explains lots details.

python acts as an intermediate for you.

But this is why I mentioned to you earlier about learning UNIX or Linux. Anytime an intermediate (interpreted usually) app does some OS good thing for you, you can control it better when you understand how something works underneath.

... followup from our earlier conversation.
 
  • Like
Likes member 428835
Thank you both! Very helpful, and Jim, I'm working on getting better with Linux. Starting with some simple shell scripts, baby steps.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 6 ·
Replies
6
Views
11K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K