Calculating average values from data file
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
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!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!