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!