how to read multiple csv files and save the data

6 views (last 30 days)
I have 10.csv, 15.csv, 20.csv ... 40 csv (total 7 files). I would like to:
  1. read 10.csv files
  2. analyze data
  3. save the analyzed data
  4. 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.

Answers (1)

Bob Thompson
Bob Thompson on 6 Feb 2018
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 Comments
Khushi Bhatti
Khushi Bhatti on 1 Jun 2018
@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
VitalKumarYadav Pillala on 26 May 2020
@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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!