Effacer les filtres
Effacer les filtres

Average data from excel

2 vues (au cours des 30 derniers jours)
Jony Smith
Jony Smith le 29 Juil 2021
Commenté : Jony Smith le 3 Août 2021
Hello! I have a lot of xls files. In the first column is the depth, in the second the speed of sound. I have a script to display them on a graph. How can I write a loop so that a graph of their average value is displayed? And the resulting average value of the depth and speed of sound was recorded in a separate xls file?
[FileName,PathName]=uigetfile('*.xls');
num=readtable([PathName FileName]);
num=table2array(num);
plot(num(:,2),num(:,1))
axis ij
grid on
hold on
  1 commentaire
Peter Perkins
Peter Perkins le 29 Juil 2021
The three files have different depth profiles, not even the same lengths. What does "average value" mean? Do you want the average speed at each depth? If so, how do you want to reconcile the different depths? Interpolation?

Connectez-vous pour commenter.

Réponses (1)

Chunru
Chunru le 29 Juil 2021
Modifié(e) : Chunru le 29 Juil 2021
fn = dir('10G*.xls');
nfiles = length(fn);
ax(1) = subplot(121); hold on;
ax(2) = subplot(122);
svp = cell(size(fn));
for i = 1:nfiles
x = readtable(fullfile(fn(i).folder, fn(i).name));
plot(ax(1), x.Var2, x.Var1);
svp{i} = [x.Var1 x.Var2];
end
axes(ax(1)); box on; grid on; ax(1).YDir ='reverse';
% average svp
depth = cellfun(@(x) x(end, 1), svp); % depth of each profile
maxdepth=max(depth);
% create new grid
z = linspace(0, maxdepth, 101);
s = zeros(size(z));
for i=1:nfiles
% interpolate the svp
s = s + interp1(svp{i}(:, 1), svp{i}(:, 2), z);
end
s = s / nfiles;
plot(ax(2), s, z);
axes(ax(2)); box on; grid on; ax(2).YDir ='reverse';
linkaxes(ax, 'xy');
  1 commentaire
Jony Smith
Jony Smith le 3 Août 2021
Thank you!

Connectez-vous pour commenter.

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by