Importing .csv and .mat files from multiples subfolders into a table

2 visualizaciones (últimos 30 días)
Emilien Tasset
Emilien Tasset el 31 de Ag. de 2021
Respondida: Vatsal el 23 de Feb. de 2024
I have a parent folder containing subfolders names 'K3Test'. In each of the 5 subfolders : I would like to extract the datas variable from the .mat files named skiinfo in a table. At the same time I need to list all the .csv files withe the '*140_measured.csv' extension from each subfolders into a cell array.
The goal would be to have a table containing all the csv files names in the first column and the corresponding skiinfo variables starting in the second, for the same root (ex : 'E0040150D6E0951D')
Here is my progress so far, but it's creating structures under structures, wich makes it harder to access datas.
Thanks a lot,
% importSkiDatas
path ='....\K3test' %enter path
selpath = uigetdir(path);
folders = dir(selpath);
folders(1:2) = []; % cut first two lines
skidata = struct;
c1=1;
c2=1;
for k=1:numel(folders)
csv_files = transpose(dir(fullfile(folders(k).folder,folders(k).name,'*140_measured.csv')));
for j=1:numel(csv_files)
skidata(c1).filename140 = csv_files(j).name; %list all csv files
c1=c1+1;
end
mat_files = transpose(dir(fullfile(folders(k).folder,folders(k).name,'*.mat')));
for i=1:numel(mat_files)
skidata(c2).datas = load(fullfile(mat_files(i).skiinfo)); %list all mat files
c2 = c2+1;
end
end
%create a table from matfiles

Respuestas (1)

Vatsal
Vatsal el 23 de Feb. de 2024
Hi,
To consolidate data from multiple subfolders into an organized table, the following script will iterate through each 'K3Test' subfolder, extract CSV file names ending with '140_measured.csv', load variables from 'skiinfo.mat' files, and compile them into a single table. This table will include the CSV file names in the first column and the corresponding 'skiinfo' data in the subsequent columns.
% importSkiDatas
path ='..\K3Test\K3Test'; %enter path
selpath = uigetdir(path);
folders = dir(selpath);
folders(1:2) = []; % cut first two lines
% Initialize cell arrays for storing csv filenames and mat data
csv_filenames = {};
mat_data = {};
for k=1:numel(folders)
csv_files = dir(fullfile(folders(k).folder, folders(k).name, '*140_measured.csv'));
mat_files = dir(fullfile(folders(k).folder, folders(k).name, '*.mat'));
for j=1:numel(csv_files)
csv_filenames{end+1, 1} = csv_files(j).name; %list all csv files
end
for i=1:numel(mat_files)
data = load(fullfile(mat_files(i).folder, mat_files(i).name)); %load mat file
if isfield(data, 'skiinfo') % check if skiinfo field exists
mat_data{end+1, 1} = data.skiinfo; %list all mat files
end
end
end
% Create a table
T = table(csv_filenames, mat_data, 'VariableNames', {'CSV_Filenames', 'Mat_Data'});
I hope this helps!

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by