- 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.
finding the average from csv file
15 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Cris LaPierre
2023-1-10
There are some mistakes with your code, but the core elements are all there.
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
8 个评论
Cris LaPierre
2023-1-12
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,:)
% Inspect the lasst 5 rows
A(end-4:end,:)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!