Unrecognized function or variable 'sum'.
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Yao Yuan Xie
le 27 Sep 2022
Réponse apportée : Steven Lord
le 27 Sep 2022
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.
0 commentaires
Réponse acceptée
Chunru
le 27 Sep 2022
Modifié(e) : Chunru
le 27 Sep 2022
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)
0 commentaires
Plus de réponses (1)
Steven Lord
le 27 Sep 2022
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.)
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!