Can't calculate mean of dataset (single column), "[F]irst argument must be numeric or logical"

조회 수: 2 (최근 30일)
I am currently trying to determine the mean, median, and mode of a given dataset.
Right now, the data is in what I believe to be a string. There should be no text in said strings, only numeric data.
The dataset in question is a "string" that has only one column full of data.
I wish to, as a start, determine the mean.
When I attempt to do so, I get this error.
M = mean(Dataset)
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);
I checked using
tf = istable
That the dataset was not a table.
May someone provide guidance as to what I am doing wrong? If I need to convert the dataset, how and to what?
Thanks.
  댓글 수: 6
dpb
dpb 2022년 9월 22일
편집: dpb 2022년 9월 22일
There should be no need for any such machinations with strings...if you imported as a MATLAB table, then the problem probably had to do with not using the variables within the table but trying to reference the table itself.
Again, attach a piece of the Excel file itself and tell us what you're trying to do -- we can't see your terminal from here and can't debug what we can't see...it is possible that sometimes a spreadsheet file can be save such as it does lead to confusion as to data types -- not terribly often, but can/does happen. Or, there are other fields in the file that don't match up -- all kinds of superfluous stuff is occasionally stuffed into a spreadsheet for auxiliary data or explanation or whatever.
Walter Roberson
Walter Roberson 2022년 9월 22일
Your reasoning does not make sense. You would not be able to take the string array and "properly plot of subplot via histfit in table format" with the string array.
You should either readmatrix() or readtable(), and if you use readtable() then use variable-name or column references as appropriate when calling functions.

댓글을 달려면 로그인하십시오.

답변 (1개)

Pravarthana P
Pravarthana P 2022년 9월 26일
Hi Austin Davis,
I understand that you are facing an issue while calculating the mean of a given dataset in ‘string’ datatype.
In order to use the ‘mean’ function the input should be in any one of the mentioned data types in this documentation link.The dataset can be converted to the required data type using "str2double" function, following which,- the mean can be calculated.
For further information kindly refer to the following documentation links:
I hope this information helps you!

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by