"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.
1 个评论
RithikaKannan K
2023-8-2
num_errors = sum(data_to_transmit ~= (received_data > 0)); help me ecode this error
回答(2 个)
Steven Lord
2017-12-11
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.
Hamza Shabbir
2021-12-14
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 个评论
Walter Roberson
2021-12-14
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)
sum(x.r) % works
% also works because all the variables in the table can be concatenated together
sum(x.Variables)
sum(x) % does not work
类别
在 帮助中心 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!