How do I extract and process data from multiple .csv Files?
34 ビュー (過去 30 日間)
古いコメントを表示
Hello!
I am doing mechanical studies an various studies and need to extract the data from various .csv files that the machine I'm using exports. Each sample results in a 7 column, variable line .csv table, in which I need to extract data from the 4th and 5th column. If I were to do them one-by-one, the code would be:
T01=readtable('1_1.csv','NumHeaderLines',2); %to ignore headers
R01=T01(:,4:5);
F01=table2array(R01);
However, I have 26 files to do this too, and I will have 60 to do next Monday, how can I automate this?
Sorry If this is a very basic question, any help is really appreciated! Thanks :D
採用された回答
Voss
2022 年 5 月 6 日
path_name = 'C:\some\directory\where\your\csv\files\are';
files = dir(fullfile(path_name,'*.csv'));
file_names = fullfile({files.folder},{files.name});
for ii = 1:numel(file_names)
T01=readtable(file_names{ii},'NumHeaderLines',2); %to ignore headers
F01=table2array(T01(:,4:5));
end
2 件のコメント
Voss
2022 年 5 月 6 日
Don't do it that way.
Instead of 26 variables, make one variable with 26 elements you can index into, e.g., a cell array:
path_name = 'C:\some\directory\where\your\csv\files\are';
files = dir(fullfile(path_name,'*.csv'));
file_names = fullfile({files.folder},{files.name});
n_files = numel(file_names);
F = cell(1,n_files);
for ii = 1:n_files
T01=readtable(file_names{ii},'NumHeaderLines',2); %to ignore headers
F{ii}=table2array(T01(:,4:5));
end
Now F{1} is the 2 columns of data from the first file, F{2} is the 2 columns of data from the second file, etc.
(You could use a 3D numeric array instead of a cell array if you're sure all the files will have the same number of rows.)
An even nicer option would be to include the data in the files struct array that you already have:
path_name = 'C:\some\directory\where\your\csv\files\are';
files = dir(fullfile(path_name,'*.csv'));
file_names = fullfile({files.folder},{files.name});
for ii = 1:numel(file_names)
T01=readtable(file_names{ii},'NumHeaderLines',2); %to ignore headers
files(ii).data = table2array(T01(:,4:5));
end
Now the file names and associated data are all together in the files variable. files(1).data is the data from the first file, files(1).name is its name, etc.
(And there may be a more direct way to read the files than readtable followed immediately by table2array, e.g., readmatrix may work.)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Import and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!