Calculating average values from data file

2 visualizzazioni (ultimi 30 giorni)
Samantha Ryan-Wheeler
Samantha Ryan-Wheeler il 18 Mar 2021
Risposto: Anudeep Kumar il 24 Apr 2025
I am importing data from a microsoft file. I need to take the average number of all the currents in that column. Any ideas on how I could do that? I need the code to import the file and also to calculate the average of all the values.

Risposte (1)

Anudeep Kumar
Anudeep Kumar il 24 Apr 2025
Hey Samantha,
Assuming your ‘.dat’ file has multiple columns (Assumed 10x3 in below code) the following code should help you achieve your goal:
filename = 'sample_multi.dat'; %Name of your file
data = importdata(filename);
% If data is a structure (due to comments), extract the numeric data
if isstruct(data)
numericData = data.data;
else
numericData = data;
end
% Calculate the average for each column
average_values = mean(numericData);
% Display the results
fprintf('Average 1st Column: %.4f\n', average_values(1));
fprintf('Average 2nd Column: %.4f\n', average_values(2));
fprintf('Average 3rd Column: %.4f\n', average_values(3));
Here is the documentation of ‘importdata’ in case you want to try different types of data with various options:
Here is a link to explanation of function ’mean’ for your reference:
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by