finding the average from csv file

Hi,
I have several csv file and each file has several columns with the name of ''x'', "y", "u", "v", "exx" like that. I want to read exx from each csv file and find the average of each. For example if I have 10 csv file then I will have 10 columns of exx so I want to find the average of ecah exx column so that at the end I will have 10 values of exx. Now I want to plot the average.
I am doing something like this but it shows concatanation error. can you please help me to figure out.
Folder='G:\Image\';
mat = dir(fullfile(Folder, '*.csv'));
% disp(mat)
% for files_i = 1:length(mat)
% disp(mat(files_i).name)
% data = fullfile(Folder,mat(files_i).name);
% ReadCSV(data,files_i);
%
% end
uall = [];
for files_i = 1:length(mat)
data = fullfile(Folder,mat(files_i).name);
x = readtable(data);
exxall =[exxall x.exx];
end
Any help will be appriciated!

 Risposta accettata

There are some mistakes with your code, but the core elements are all there.
  • create exxall as an empty array. You currently create an unused variable, uall
  • You csv files do not all have the same length, so you will get an error when trying to concatenate them. Create exxall as the mean of x.exx to get around this.
Folder='./';
mat = dir(fullfile(Folder, '*.csv'));
exxall = [];
for files_i = 1:length(mat)
data = fullfile(mat(files_i).folder,mat(files_i).name);
x = readtable(data);
exxall =[exxall mean(x.exx)];
end
exxall
exxall = 1×10
0 0.0192 0.0212 0.0084 0.0052 0.0224 0.0176 0.0113 0.0073 0.0270

8 Commenti

Thank you very much. It worked.
I want to implement the following code into a for loop to all the above attached csv files. I am trying to do this but I am getting an error. Can you plesae help me out. I did it for 1 file and its working fine but trying to do it for all the attached files.
Folder='G:\
Data = readtable('0001.csv');
V = Data.u;
T = Data.x;
A=diff(V)./diff(T);
What is the error message you are getting?
Raushan
Raushan il 12 Gen 2023
Modificato: Raushan il 12 Gen 2023
I used this for 1 files.
( Folder='G:\
Data = readtable('0001.csv');
V = Data.u;
T = Data.x;
A=diff(V)./diff(T);) here I have 1 set of A, I will find the mean of A later. Now i tried doing this for all the 10 files to get 10 different A using for loop but I am getting dimensions error.
Folder='G:\
mat = dir(fullfile(Folder, '*.csv'));
uall = [];
for files_i = 1:length(mat)
data = fullfile(Folder,mat(files_i).name);
x = readtable(data);
uall = [uall x.u];
end
xall = [];
for files_i = 1:length(mat)
data = fullfile(Folder,mat(files_i).name);
X = readtable(data);
xall = [xall X.x];
end
A=diff(uall)./diff(xall);
Cris LaPierre
Cris LaPierre il 12 Gen 2023
Modificato: Cris LaPierre il 20 Gen 2023
It is more helpful to copy/paste the full error message (all the red text) along with the code you ran that produced the error.
Yes, I mentioned that you would get a dimension error in my first reply. Your files do not all have the same number of rows, so trying to concatenate them together will result in an error. That's why I took the mean directly.
I got that. Thank you very much for the suggestion though. I just saw the minimum size of my files and then indexed all the other size as per it and it worked. I am writting the script here for all the other users who face this types of problem. I am excited and very much interested to learn the Matlab just becuase of help that I am getting from you guys. Please keep supporting us.
Folder='G:\'
mat = dir(fullfile(Folder, '*.csv'));
uall = [];
xall = [];
for files_i = 1:length(mat)
data = fullfile(Folder,mat(files_i).name);
X = readtable(data);
uall = [uall diff(X.u(1:1336))];
xall = [xall diff(X.x(1:1336))];
end
A = uall./xall;
That is one approach. Here's another.that doesn't require predetermining the number of rows to use. By specifying the rows to assign to, it automatically increases the size of the array. When it does this, it adds NaN to the other columns.
NaNs can have an impact when taking a mean, so be sure to set the nanflag property of mean to either include or omit nans in the calculation.
mat = dir('*.csv');
uall = [];
xall = [];
for files_i = 1:length(mat)
X = readtable(mat(files_i).name);
uall(1:height(X)-1,files_i) = diff(X.u);
xall(1:height(X)-1,files_i) = diff(X.x);
end
A = uall./xall;
% Inspect the first 5 rows
A(1:5,:)
ans = 5×10
0 0.0314 0.0170 0.0017 0.0063 0.0262 0.0123 0.0039 -0.0037 -0.0018 0 0.0326 0.0197 0.0111 0.0121 0.0200 0.0131 0.0088 0.0078 0.0184 0 0.0282 0.0187 0.0136 0.0113 0.0208 0.0195 0.0106 0.0126 0.0180 0 0.0253 0.0206 0.0172 0.0143 0.0191 0.0202 0.0144 0.0152 0.0191 0 0.0152 0.0158 0.0164 0.0123 0.0174 0.0139 0.0133 0.0056 0.0180
% Inspect the lasst 5 rows
A(end-4:end,:)
ans = 5×10
0 0.0105 0.0203 0.0158 0.0072 0.0113 0.0162 0.0157 0.0149 0.0233 0 0.0015 0.0026 0.0102 0.0066 0.0161 0.0196 -0.0056 0.0021 0.0345 0 0.0229 0.0126 0.0016 -0.0044 0.0329 0.0037 -0.0040 0.0035 0.0275 0 0.0018 0.0012 0.0055 0.0015 0.0265 0.0131 NaN NaN 0.0205 0 0.0081 0.0049 NaN NaN 0.0329 NaN NaN NaN 0.0251
Thank you so much for showing me another method too. I really appreciate this.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by