How to fix an invalid data type

조회 수: 27 (최근 30일)
A
A 2019년 9월 12일
댓글: Rik 2020년 11월 18일
This is my script,
%This script calculates some statistics for the wine data
load('winedata');
A=winedata; %matrix
max=max(A);
min=min(A);
sigma=std(A);
mean=mean(A);
% End of script
Error using max
Invalid data type. First argument must be numeric or logical.
Error in Wine_Data (line 7)
max=max(A);
How would I fix this?
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 9월 12일
What shows up for class(A) ?
Rik
Rik 2020년 11월 18일
Why did you flag your own question as unclear? (just to make sure you don't try anything funny: here's an archive)

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

답변 (2개)

David Hill
David Hill 2019년 9월 12일
Max=max(A)%do not name variable max or min
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 9월 12일
This is a good idea, but unfortunately in itself it will not solve the problem.

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


J Chen
J Chen 2019년 9월 12일
The winedata may contain some strange characters. Carefully exame elements of windata. Command class(A) should return 'double'.
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 9월 12일
When you do
mean=mean(A);
the first time, then there is no variable named mean so MATLAB looks along the search path and finds a function named mean and uses the function. Then you are assigning the result to the variable mean
The second time through, you have a variable named mean so mean(A) is interpreted as trying to index into the variable.
With the script you posted, if you only execute the script once, then you do not have a problem in that code immediately, but you do leave variables mean and min and max in the base workspace, where they will cause problems if you try to execute the script again, or try do use any of those three at the command line or in another script.
You should learn to use functions, and you should avoid using variables that are the same name as any of the common library routines.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by