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

1 view (last 30 days)
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
df1=pd.read_csv('file1.txt',names=['x1','Y1','Value1'],nrows=5356)
df2=pd.read_csv('file2.txt',names=['x2','Y2','Value2'],nrows=5356)
df3=pd.read_csv('text3.txt',names=['x3','Y3','Value3'],nrows=5356)
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))

Accepted Answer

Les Beckham
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):
% simulating read of csv file1 (replace by data1 = readmatrix('file1.csv')
data1 = ...
[1 0 -98
2 0 -94
3 0 -93];
% simulating read of csv file2 (replace by data2 = readmatrix('file2.csv')
data2 = ...
[1 0 -97
2 0 -95
3 0 -92];
% simulating read of csv file3 (replace by data3 = readmatrix('file3.csv')
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 Comments
Les Beckham
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
data = readmatrix(fullfile(files(i).folder, files(i).name));
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')

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!