how to read multiple csv files and save the data
이전 댓글 표시
I have 10.csv, 15.csv, 20.csv ... 40 csv (total 7 files). I would like to:
- read 10.csv files
- analyze data
- save the analyzed data
- read the next csv file, 15.csv file ... 40.csv so on.
This is what I vaguely format:
for k = ...
(read *.csv file)
(analyze data)
(save data)
end
(next file)
so the outputs are accumulating after each file.
답변 (1개)
Bob Thompson
2018년 2월 6일
Before you start the for loop, use uigetfile() to select all of the .csvs.
[filename,filedir] = uigetfile('*.csv','Multiselect','on');
path = fullfile(filedir,filename);
path = path';
file = struct('name',path); % I like to add these last two lines so I can just call the structure, instead of fighting with other types of values.
Then all you have to do is set up your for loop to cycle through all the files selected by using file(k).name as your file name variable.
댓글 수: 2
Khushi Bhatti
2018년 6월 1일
@BobNbob thank you so much i think u have solved my issue.my code actually run but i can't understand the logic.can you please explain what you did in this code?
VitalKumarYadav Pillala
2020년 5월 26일
@BobNbob, I have some number of csv files from data logger. Which I need to read into matlab first
and then in each csv file i have 1024 rows and 3 colums for which i need to take mean and then write to a new file.
I used below for csv from the folder and see all the files in the workspace in results as 16x1 cell array, but not able to read the data inside and do the mean of the columns on each file.
I am unable to undertsnad what happening can you please help me by explaining and correcting the code.
files = dir('*.csv');
num_files = length(files);
results = cell(length(files), 1);
for i = 1:num_files
results{i} = xlsread(files(i).name);
M = reshape(results,1,16)
end
for i = 1:16
M1= zeros(1024,3);
M1(i,:)= results{(i),1};
A1(i)= sum(M1);
end
카테고리
도움말 센터 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!