# How to do the example below in MatLab? I am dealing with same sort of problem in matlab

3 views (last 30 days)
muhammad choudhry on 16 Jun 2022
Commented: Les Beckham on 17 Jun 2022
Problem:
i have 3 csv files named file1, file2, file3. Each CSV is filled with 3 Columns and 5653 rows:
1 0 -95
2 0 -94
3 0 -93
...
51 0 -93
0 1 -92
1 1 -91
2 1 -90
First column is a X variable 2nd is a y variable, 3rd is a measured value from which I want to have the mean.
What I want to do is:
• read first row of file 1
• read first row of file 2
• read first row of file 3 and then count the mean of the measured value.
So for example:
file1 row1 -98
file2 row1 -97
file3 row1 -95
mean 96,666666667
i want to write that mean into a new csv file with the following format
1,0,mean_of_row1 (which would be 96,666666667)
2,0,mean_of_row2
3,0,mean_of_row3
4,0,mean_of_row4
Solution in Python:
import pandas as pd
df_concat= pd.concat([df1,df2,df3], axis=1)
print df_concat
df_concat['meanvalue']=df_concat[['Value1','Value2','Value3']].mean(axis=1)
print(df_concat.to_csv(columns=['meanvalue'],index=False))

Les Beckham on 17 Jun 2022
Edited: Les Beckham on 17 Jun 2022
Something like this should do what you want (using made up example data):
data1 = ...
[1 0 -98
2 0 -94
3 0 -93];
data2 = ...
[1 0 -97
2 0 -95
3 0 -92];
data3 = ...
[1 0 -95
2 0 -93
3 0 -91];
% calculate the means of the third column of each of these datasets
mean_data = mean([data1(:,3) data2(:,3) data3(:,3)], 2)
mean_data = 3×1
-96.6667 -94.0000 -92.0000
output = [(1:numel(mean_data))' zeros(size(mean_data)) mean_data]
output = 3×3
1.0000 0 -96.6667 2.0000 0 -94.0000 3.0000 0 -92.0000
% then use writematrix to create your output file:
% writematrix(output, 'meandata.csv')
If this doesn't provide the help you need and you have code that you created that is not working and have a specific problem with it, please post it (as text, not as a screenshot) and explain what the problem is. If you are getting an error message, post the entire error message (all of the red text)
##### 2 CommentsShowHide 1 older comment
Les Beckham on 17 Jun 2022
You need to collect up all of the desired columns inside the loop and then take the mean after the loop.
Here is an example.
% Create the test data
data1 = ...
[1 0 -98
2 0 -94
3 0 -93];
writematrix(data1, 'file1.csv');
data2 = ...
[1 0 -97
2 0 -95
3 0 -92];
writematrix(data2, 'file2.csv');
data3 = ...
[1 0 -95
2 0 -93
3 0 -91];
writematrix(data3, 'file3.csv');
data4 = ...
[1 0 -95
2 0 -93
3 0 -91];
writematrix(data4, 'file4.csv');
clearvars % clear the variables so we can read it from the files
% Now read the files and do the calculations
files = dir('*.csv');
numrows = 3; % in your case it would be 48
numfiles = numel(files); % in this example 4, in your case 79
col3 = zeros(numrows, numfiles); % container for all of the column 3 data (in your case, column 13)
for i=1:numfiles
col3(:,i) = data(:,3); % extract the desired column
end
% calculate the means of the third column of each of these datasets
mean_data = mean(col3, 2)
mean_data = 3×1
-96.2500 -93.7500 -91.7500
output = [(1:numel(mean_data))' zeros(size(mean_data)) mean_data] % create output array (modify to your desired format)
output = 3×3
1.0000 0 -96.2500 2.0000 0 -93.7500 3.0000 0 -91.7500
% then use writematrix to create your output file:
% writematrix(output, 'meandata.csv')