"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
Mostrar comentarios más antiguos
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 comentario
RithikaKannan K
el 2 de Ag. de 2023
num_errors = sum(data_to_transmit ~= (received_data > 0)); help me ecode this error
Respuestas (2)
Steven Lord
el 11 de Dic. de 2017
2 votos
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
el 14 de Dic. de 2021
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 comentarios
Walter Roberson
el 14 de Dic. de 2021
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
Categorías
Más información sobre Cell Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!