Unrecognized function or variable 'sum'.

조회 수: 31 (최근 30일)
Yao Yuan Xie
Yao Yuan Xie 2022년 9월 27일
답변: Steven Lord 2022년 9월 27일
classdef question2
methods
function q2a = l1qa(~,table)
clear sum;
z = zeros(height(table),2);
for i = 2:height(table)
z(i,1) = i;
temp = table(i,3:6);
z(i,2) = sum(temp{:,1:4});
%z(i,2) = temp{1,1} + temp{1,2} + temp{1,3} + temp{1,4};
end
[sum,index] = max(z(:,2));
table(index,1)
output = "Name: " + table{index,1} + newline +"Mark: " + sum;
q2a = output;
end
end
end
opts = detectImportOptions('course_grades_2022.xlsx');
opts = setvartype(opts, {'ID_Number', 'Name'}, 'string');
table = readtable('course_grades_2022.xlsx', opts);
g = question2();
g.l1qa(table)
Unrecognized function or variable 'sum'.
Error in question2/l1qa (line 10)
z(i,2) = sum(temp{:,1:4});
Error in test1 (line 6)
g.l1qa(table)
This is the error I'm getting after trying to implement my code as a function in a class. It seems that matlab is trying to evaluate sum as a variable instead of a function. I have tried clearing sum but it does not fix the problem. I am new to matlab and any help would be appreciated.

채택된 답변

Chunru
Chunru 2022년 9월 27일
편집: Chunru 2022년 9월 27일
classdef question2
methods
function q2a = l1qa(~,table)
% clear sum;
s = 0; % don't use the function name as variable
z = zeros(height(table),2);
for i = 2:height(table)
z(i,1) = i;
temp = table(i,3:6);
z(i,2) = sum(temp{:,1:4});
%z(i,2) = temp{1,1} + temp{1,2} + temp{1,3} + temp{1,4};
end
%[sum,index] = max(z(:,2));
[s,index] = max(z(:,2));
table(index,1)
% output = "Name: " + table{index,1} + newline +"Mark: " + sum;
output = "Name: " + table{index,1} + newline +"Mark: " + s;
q2a = output;
end
end
end
opts = detectImportOptions('course_grades_2022.xlsx');
opts = setvartype(opts, {'ID_Number', 'Name'}, 'string');
table = readtable('course_grades_2022.xlsx', opts);
g = question2();
g.l1qa(table)

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 9월 27일
It seems that matlab is trying to evaluate sum as a variable instead of a function.
That is correct, because in the method it is a variable. Looking at part of your code:
function q2a = l1qa(~,table)
clear sum;
z = zeros(height(table),2);
for i = 2:height(table)
z(i,1) = i;
temp = table(i,3:6);
z(i,2) = sum(temp{:,1:4});
%z(i,2) = temp{1,1} + temp{1,2} + temp{1,3} + temp{1,4};
end
[sum,index] = max(z(:,2));
On this line of code you assign a value to the variable sum. That means that in this function the identifier sum must be a variable. Your use in the for loop is therefore an attempt to index into that variable before it's actually defined.
You cannot use the same identifier as both a variable and a function in the same function, even if you refer to that identifier before you create the variable. Clearing the variable won't change the fact that MATLAB saw the assignment to the variable when it parsed the code and so decided at that time that sum is a variable in this function.
I'd recommend changing the name of your variables sum (so it doesn't conflict with the sum function) and table (which already has a meaning in MATLAB.)

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by