How to read multiple csv files and do calculations

4 views (last 30 days)
HI
I have a 30 csv files containing data like this. naming of all the files are in sequence.. i.e. DAT001.csv, DAT002.csv,.....,DAT030.csv
3
1ms 1kA 10V
2ms 2.2kA 3V
3ms 3.6kA 5.4V
I want to make a code which includes
first: reading all the csv files.
2nd : read the col 1 row 1 and store in a variable(say n).
3rd : make a 3x3 matrix from row 1 to 3 and col 1 to 3 with removing 'ms', 'kA' & 'V' value.
I have done the 2nd and third point. Can you explain how to do the first job point.
Code i used for second and 3rd point (*it uses a excel file*):
Bo ='Book3.xlsx'; % Alloting the excel file
[num,txt,raw] = xlsread(Bo); %reading the excel file and storing in a cell array named 'raw'
n=raw(1,1); % Giving weld time in a cell array of 1x1
n=cell2mat(n); % converting n into a matrix value
DCR = raw(2:4,:); % creating a cell array for DCR with time, cuurent and voltage value
DCR(:,2)=[]; % deleting 2nd column with value '*'
DCR=string(DCR); % convering DCR cell array to ta string array
DCR(:,1)=erase(DCR(:,1),'ms'); % erasing units in DCR string array
DCR(:,2)=erase(DCR(:,2),'kA');
DCR(:,3)=erase(DCR(:,3),'V');
DCR=double(DCR) % converting string array to a numeric array

Answers (1)

Andreas Kvalbein Fjetland
Andreas Kvalbein Fjetland on 30 Jan 2019
Edited: Andreas Kvalbein Fjetland on 30 Jan 2019
Hi Gagan,
I think the datastore function could help you out here. The code below will open all compatible files in the subfolder 'test' and loop trough them. In this instance to store each table to a structure. You could of course do any operation you want in there. The read(tabsds) command gives out the data as tables in sequential order.
tabsds = datastore('test/*');
i=1;
while hasdata(tabsds)
s(i).tbl = read(tabsds);
i=i+1;
end
disp(s(1).tbl)
Look into the help page for datastore and you can find some more useful tips and tricks with it.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!