Error using sum Invalid data type. First argument must be numeric or logical. - the data I am using is numerical ??
20 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Dyuman Joshi
2023-3-31
编辑:Dyuman Joshi
2023-3-31
"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)
std = std(data)
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Prepare Data 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!