Average data from excel

2 次查看(过去 30 天)
Jony Smith
Jony Smith 2021-7-29
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 个评论
Peter Perkins
Peter Perkins 2021-7-29
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?

请先登录,再进行评论。

回答(1 个)

Chunru
Chunru 2021-7-29
编辑:Chunru 2021-7-29
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');

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by