MATLAB Answers

0

How to find out the average of data gathered from a For Loop for a specific column in a table?

Asked by sebastian lim on 13 Feb 2019
Latest activity Commented on by sebastian lim on 14 Feb 2019
The script is finding out the mean pixel intensities in a ROI as well as other properties of sequential images in the folder.
I am trying to find out how to find the average of a certain column (meanGL) in the table T that is created from data gathered in the For Loop, this part of the code is not working. I am trying to find the average of the meanGL values.
% Creates table from For Loop Values with separate column for each variable
T.Properties.VariableNames={'numberOfPixels1' 'numberOfPixels2 ' 'meanGL'};
%Calculate the average meanGL for those specific layers
B = T(:,[3])
meanVal = mean(B,1)
% Name the file
filename='Thermal Imaging Data.xlsx'
% Write the data into named excel spreadsheet
writetable(T,filename,'Sheet',1,'Range','B2')
The whole script I am using is attatched.
Also if this average could also be displayed in the same excel spreadsheet as the rest of the data that would be wonderful?

  4 Comments

Show 1 older comment
The code error that I get is:
Error using sum
Invalid data type. First argument must be numeric or
logical.
Error in mean (line 116)
y = sum(x, dim, flag) ./ size(x,dim);
Error in Attempt_File3 (line 53)
meanVal = mean(B,1)
Yes that is correct i am trying to get a single average value of the mean results. But at the moment i just get this error.
Thank you for the help.
Not 100% sure but i think its a column of just the meanGL values. And its a table.
Got it working now using the code:
meanVal = mean(T.meanGL,1);
This was what i was trying to achieve. Realise i didnt explain it very well.

Sign in to comment.

1 Answer

Answer by Peter Perkins
on 13 Feb 2019
 Accepted Answer

The problem is that you are passing a table into mean. The table is a container, and what you need to do is to pass the variable in the table to mean. A one-variable table is not the same thing as the one variable.
You have variable names, you should use them!
meanVal = mean(T.meanGL,1);
There's more than one mean going on there, I guess, but I think that does what you are trying to do.

  1 Comment

Thank you very much, i understand the problem. That makes it much clearer! And yes that code is what i am trying to do. Your help is much appreciated.

Sign in to comment.