How to plot the mean and standard deviation of collected data
    84 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
Hi!
I've been trying to use raacampbell's shadedErroBar (attached) to plot some data that I've collected. I have three different data files that I import (load and extension data), and then I was trying to find the mean and standard deviation. I'm trying to generate a graph that looks like the following:

I've attached my code, the shadedErroBar code, and some of the data files that I've been using. The current code throws the error:
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean (line 127)
y = sum(x, dim, flag) ./ mysize(x,dim);
Error in materials_curve (line 40)
m_load = mean(l);
Sorry if this is a really introductory question, I'm just really rusty on my data processing/plotting skills. Thank you so much! The code is also below:
clear all; close all; clc;
colors = 1/255*[158,1,66 ; 213,62,79; 244,109,67; 253,174,97; ...
    254,224,139; 255,255,191; 230,245,152; 171,221,164; 102,194,165; 50,136,189; 94,79,162 ];
files = dir('med*.csv');
n = length(files);
figure(1); clf;
l = [];
e = [];
for i = 1:n
    data = readtable(files(i).name);
    load = data(5:2400,3);
    ext = data(5:2400,2);
    ltable = table2array(load);
    etable = table2array(ext);
    l = [l ltable];
    e = [e etable];
end
m_load = mean(l);
m_ext = mean(e);
std_1 = std(l);
max_load = mean(max(l));
A = max(l);
std_2 = std(A);
ph = shadedErrorBar(m_ext, m_load, std_1);
set(ph.mainLine, 'LineWidth', 1, 'Color', colors(1,:))            % modify the mean line 
set(ph.edge, 'linewidth', 0.5, 'color', [colors(1,:) 0.1])                         % modify edge lines (+/-) 1 std dev lines
set(ph.patch, 'facecolor', colors(1,:), 'facealpha', 0.5)           % modify shaded region
xlabel('Extension')
ylabel('Load (kN)')
0 commentaires
Réponse acceptée
  dpb
      
      
 le 18 Oct 2022
        I don't get any such error if I just work at the command line...
i1=5; i2=2400;
tT1=readtable('med_tube_19_187g_1.csv');
tT2=readtable('med_tube_21_96g_1.csv');
l=[tT1.Force(i1:i2) tT2.Force(i1:i2)];
m_load=mean(l);
resulted in
>> m_load
m_load =
   11.5600   12.8750
>> 
I don't see anything that looks like it should cause the problem in the posted code -- it is somewhat verbose and in particular, as shown above, there's no point in using readtable if you're just going to turn everything back into a copy of the same data as an array; use the table variables instead.
The catenation by dynamic reallocation is inefficient, but for only three pretty small datasets it's not going to be worth fooling with too much; if you take this up to a much bigger dataset size, however, then it may become an issue. (I missed the third first time; did add it to make sure it wasn't the culprit; it's all numeric data, too).
What happens later I dunno; note that you're computing the mean of the columns, looks like this is supposed to be the mean of the three columns by row, not the overall means -- that means you need the mean and std computations to be
m_load=mean(l,2);       % compute by row, not the column means
std_1 = std(l,[],2);    % ditto, note the second argument is the divisor weight, not dim
and similar, of course, for the other variables.
Maybe that will get you past your hurdle; I don't want to mess with downloading a FEX routine to go any further....doubt that it's your problem when you pass it the right data.
4 commentaires
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


