フィルターのクリア

Average data from excel

2 ビュー (過去 30 日間)
Jony Smith
Jony Smith 2021 年 7 月 29 日
コメント済み: Jony Smith 2021 年 8 月 3 日
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');
  1 件のコメント
Jony Smith
Jony Smith 2021 年 8 月 3 日
Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by