How do you plot the average of a group of lines in a figure?

3 views (last 30 days)
Jacob Capito
Jacob Capito on 10 May 2022
Answered: Chunru on 11 May 2022
I have a group of lines that represent the damping of a system for a given mode and I want to find the average trendline just from using the data given to me in the graph. I don't have the x and y datapoints and it would be a pain to try to go get them. Is there a way to make an average trendline just from the lines given to me in the figure?
The only things I have available to me are a vector of object type "line" from the figure and the figure itself
  3 Comments
Image Analyst
Image Analyst on 10 May 2022
I'd rather have the original data, not a .fig file visualization of it. Can you attach the original data in a .mat file or text file? Then I'd just interpolate all the curves on a common x-axis, add up all the values and divide by the number of curves.

Sign in to comment.

Accepted Answer

Chunru
Chunru on 11 May 2022
h = openfig("Damping_mode_2.fig", "visible");
hl = findobj(h, 'Type', 'Legend');
hl.Location = 'southoutside'; hl.NumColumns = 4; hl.FontSize = 5;
hlines = findobj(gca, 'Type', 'Line');
for i=1:length(hlines)
x{i,1} = hlines(i).XData;
%size(x{i})
y{i,1} = hlines(i).YData;
name{i,1} = hlines(i).DisplayName;
end
% convert to matrix
x = cell2mat(x)';
y = cell2mat(y)';
xmin = min(x(:)); xmax=max(x(:));
xg = linspace(xmin, xmax, 51);
yg = zeros(length(xg), size(x,2));
% interpolate the data
for i=1:size(x, 2)
yg(:, i) = interp1(x(:,i), y(:,i), xg, 'linear', 'extrap');
end
ymean = mean(yg, 2);
hold on
plot(xg, ymean, 'r', 'LineWidth', 4, 'DisplayName', 'mean');

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by