Average data from excel
2 views (last 30 days)
Show older comments
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 Comment
Peter Perkins
on 29 Jul 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?
Answers (1)
Chunru
on 29 Jul 2021
Edited: Chunru
on 29 Jul 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');
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!