Calculating average values from data file
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Anudeep Kumar
on 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!
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!