read multiple csv files from different subfolders
    18 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
I have a folder, "Raw data", which contains subfolders for subjects 005 - 016. In each subfolder, I want to choose a subsubfolder, "A". In "A" folder, there are 23 .csv files. How can I read them as individual file (without merging them)  at once, maybe using a loop function of other methods? 
0 Commenti
Risposte (3)
  Four Eyes
 il 13 Ott 2023
        
      Modificato: Four Eyes
 il 13 Ott 2023
  
      This might be coming in too late. But I recently ran accross a similar problem and here is what that worked for me. Hopefully it is useful to someone!
Suppose you have Fodler 1 stored somewhere like C:\Users\username...\Folder1\....
And You have multiple subfolders like this:
C:\Users\username...\Folder1\Subfolder1\Random_name1.csv;      C:\Users\username...\Folder1\Subfolder1\Random_name2.csv; 
C:\Users\username...\Folder1\Subfolder2\Random_name3.csv;       C:\Users\username...\Folder1\Subfolder2\Random_name4.csv; 
and so on
%To read all the file names, do this:
location = C:\Users\username...\Folder1\**\;
files= dir([location '*.csv'])
%To read the data and store in a matrx, do this:
for ii = 1:length(files)
    data(:,:,ii) = readmatrix([files(ii).folder '\' files(ii).name]);
end
1 Commento
  Image Analyst
      
      
 il 13 Ott 2023
				That's similar to what's in the FAQ: https://matlab.fandom.com/wiki/FAQ#How_can_I_process_a_sequence_of_files?
% To read all the file names, do this:
location = "C:\Users\username...\Folder1\**\";
filePattern = fullfile(location, "*.csv")
files = dir(filePattern)
% To read the data and store in a 3-D array, do this:
% (NOTE: all data must be the same size 2-D matrix).
for ii = 1 : length(files)
    fullFileName = fullfile(files(ii).folder, files(ii).name);
    fprintf('Processing %d of %d : "%s".\n' ii, numel(files), fullFileName)
    data(:,:,ii) = readmatrix(fullFileName);
end
  prasanth s
      
 il 17 Ott 2019
        
      Modificato: prasanth s
      
 il 14 Dic 2022
  
      Use 'dir' function to get the directory and file list of any folder. e.g
D=dir('Raw data');
output 'D' is an structure array contains all foldernames. 
use following code to get all csv filenames
A=dir('myfolder\*.csv');
get the filenames from 'A' and 
use 'csvread' function to read each csv files
0 Commenti
  Image Analyst
      
      
 il 14 Dic 2022
        Try this:
% Specify top level folder and file pattern.
topLevelFolder = pwd; % Or whatever you want.
filePattern = fullfile(topLevelFolder, '*.csv')
% Get a list of all files in that folder and within its subfolders.
ds = fileDatastore(filePattern, 'ReadFcn', @readmatrix)
allFileNames = ds.Files
0 Commenti
Vedere anche
Categorie
				Scopri di più su File Operations in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



