Error using sum Invalid data type. First argument must be numeric or logical. - the data I am using is numerical ??

28 visualizaciones (últimos 30 días)
I am trying to calculate the mean of the data I have imported and combined. However, for some reason it says my data is not numerical which it is. It gives me this 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 Coursework_Part_1 (line 99)
mu = mean(data);
Here is my Matlab script:
% reading the data files into Matlab
drugs20 = readtable("drugs20.txt");
placebo20 = readtable("placebo20.txt");
drugs30 = readtable("drugs30.txt");
placebo30 = readtable("placebo30.txt");
% combine the drug and placebo data as 40 participants are now selected at
% random from the initial participant pool
drugs_data = {drugs20(:,1) ; drugs30(:,1)};
placebo_data = {placebo20(:,1) ; placebo30(:,1)};
% compile all data into one list
data = {drugs_data ; placebo_data};
% Estimate the mean and the standard deviation using maximum likelihood
% estimation
mu = mean(data);
std = std(data);
I have also attachd my data sets. If someone could help this would be much appreciated. I have tried to use previous people's answers for this issue but it does not seem to work.
  2 comentarios
Torsten
Torsten el 31 de Mzo. de 2023
Editada: Torsten el 31 de Mzo. de 2023
"data" is a 2x1 cell array of two cell arrays. You first have to go back to numerical arrays to apply numerical functions to it. So you should consider whether writing your data in this complicated structure make sense in view of a data analysis.

Iniciar sesión para comentar.

Respuestas (1)

Dyuman Joshi
Dyuman Joshi el 31 de Mzo. de 2023
Editada: Dyuman Joshi el 31 de Mzo. de 2023
"However, for some reason it says my data is not numerical which it is."
In the context of MATLAB, your data is in fact not numerical. The data type of variable "data" is cell, which you can check by using the command class(data) after defining data.
As the text files only contain numeric data, use readmatrix to maintain the data type as numeric -
% reading the data files into Matlab
drugs20 = readmatrix("drugs20.txt");
placebo20 = readmatrix("placebo20.txt");
drugs30 = readmatrix("drugs30.txt");
placebo30 = readmatrix("placebo30.txt");
% combine the drug and placebo data as 40 participants are now selected at
% random from the initial participant pool
Use [ ] to concatenate the variables and preserve the numeric data type
drugs_data = [drugs20(:,1) ; drugs30(:,1)];
placebo_data = [placebo20(:,1) ; placebo30(:,1)];
% compile all data into one list
data = [drugs_data ; placebo_data];
% Estimate the mean and the standard deviation using maximum likelihood
% estimation
mu = mean(data)
mu = 8.4502
std = std(data)
std = 1.3072

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by