Calculating average values from data file
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Anudeep Kumar
le 24 Avr 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 commentaires
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!