Calculating average values from data file

2 ビュー (過去 30 日間)
Samantha Ryan-Wheeler
Samantha Ryan-Wheeler 2021 年 3 月 18 日
回答済み: Anudeep Kumar 2025 年 4 月 24 日
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.

回答 (1 件)

Anudeep Kumar
Anudeep Kumar 2025 年 4 月 24 日
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!

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by