How to plot the mean and standard deviation of collected data

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)')

 Accepted Answer

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 Comments

Thank you, super helpful! For whatever reason, I still get the same error when I try to run your code that computes the mean straight from reading from the table. Do you think I need to update my software or something?
Well, I downloaded your script as well as the FEX submission and only changing the definition of the mean/std to work on second dimension ran successfully...
That code is
% Plots materials data from instron
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
% should convert l and e to stress and strain for a good plot
% stress = force/area
% strain = change in length/original length (in this case, height of
% lattice) (height-e)/height
m_load = mean(l,2);
m_ext = mean(e,2);
std_1 = std(l,[],2);
max_load = mean(max(l));
A = max(l);
std_2 = std(A);
ph = shadedErrorBar(m_ext, m_load, std_1);
%use the plot handles in the ph struct to modify the plot use
%get(ph.*****) to see what properties you can modify!
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)')
still using the extra arrays and all...I can't see anything in this code that should be release related unless you're using something that is really, really, really old...and even there, I don't see anything here that looks to be something different over last 20 years....
Try the above and post the entire error message in context if something still goes south...
ADDENDUM:
I also streamlined the original to use arrays and not the table and removed the extra unneeded manipulations and got the same result --
% Plots materials data from instron
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=readmatrix(files(i).name);
data=data(5:2400,:);
e = [e data(:,2)];
l = [l data(:,3)];
end
% should convert l and e to stress and strain for a good plot
% stress = force/area
% strain = change in length/original length (in this case, height of
% lattice) (height-e)/height
m_load = mean(l,2);
m_ext = mean(e,2);
std_1 = std(l,[],2);
max_load = mean(max(l));
A = max(l);
std_2 = std(A);
ph = shadedErrorBar(m_ext, m_load, std_1);
%use the plot handles in the ph struct to modify the plot use
%get(ph.*****) to see what properties you can modify!
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)')
Weird, works totally fine now! Thank you so much!
You had something not quite right; no telling what, precisely...

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Asked:

on 17 Oct 2022

Commented:

dpb
on 18 Oct 2022

Community Treasure Hunt

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

Start Hunting!