"Error using sum: Invalid data type. First argument must be numeric or logical." But the data inside the cell array I'm referencing IS numeric

I'm trying to calculate sums on variables that I created by pulling numbers out of my raw data read in from an xls file. For example in my code: Mindset1 = Raw(2,17:36);
When I type Mindset1 into the command line, here's what's returned Mindset1 =
1×20 cell array
Columns 1 through 18
[2] [3] [2] [2] [2] [1] [2] [2] [2] [2] [2] [1] [2] [2] [2] [2] [2] [2]
Columns 19 through 20
[2] [2]
So next what I try to do is run a sum on that variable. In my code: SUM_Mindset1 = sum(Mindset1);
And I keep getting this error when I try to run it:
Error using sum Invalid data type. First argument must be numeric or logical.
Error in DATAANALYSIS2 (line 26) SUM_Mindset1 = sum(Mindset1);
Thanks.

回答(2 个)

You may have a T-shirt inside your clothes dresser, but you can't wear your dresser. [At least not without some significant modifications involving a saw or removing the drawers then resting the inside of the top of the dresser on your head, which would be somewhat uncomfortable.]
Similarly, you may have double precision data inside a cell array, but you can't sum your cell array. You would need to convert the cell array into a double array using cell2mat or something similar.
encountered similar problem. Sum works on Load but not when we readtable due to datatype error.
data=load("emg_all_features_labeled.csv");
data=readtable("emg_all_features_labeled.csv")

2 个评论

load() of a csv file returns a numeric array.
readtable() never returns a numeric array. It returns a table() data structure.
You should use readmatrix() rather than readtable()
Or read the data into a table array and perform sum on the variable from the table, not the table itself.
r = randperm(10).';
x = table(r)
x = 10×1 table
r __ 4 7 1 10 9 5 2 3 8 6
sum(x.r) % works
ans = 55
% also works because all the variables in the table can be concatenated together
sum(x.Variables)
ans = 55
sum(x) % does not work
Error using sum
Invalid data type. First argument must be numeric or logical.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by