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 件のコメント
Torsten
Torsten 2023 年 3 月 31 日
編集済み: Torsten 2023 年 3 月 31 日
"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.
Archie Baxter
Archie Baxter 2023 年 3 月 31 日
It a coursework for University so I have to combine it unfortunately

サインインしてコメントする。

回答 (1 件)

Dyuman Joshi
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)
mu = 8.4502
std = std(data)
std = 1.3072
  2 件のコメント
Archie Baxter
Archie Baxter 2023 年 3 月 31 日
Thank you, this has solved it!
Dyuman Joshi
Dyuman Joshi 2023 年 4 月 1 日
Please accept my solution if it solved your problem, Thank you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePrepare Data についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by