Azzera filtri
Azzera filtri

Average data from excel

2 visualizzazioni (ultimi 30 giorni)
Jony Smith
Jony Smith il 29 Lug 2021
Commentato: Jony Smith il 3 Ago 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 Commento
Peter Perkins
Peter Perkins il 29 Lug 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?

Accedi per commentare.

Risposte (1)

Chunru
Chunru il 29 Lug 2021
Modificato: Chunru il 29 Lug 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');

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by