Why won't my function display any of the required text?

The function / script runs completely fine except for the fact that it won't print any of the disp(....) text form inside the function. If I remove the function start and end block and rerun it as a normal script it runs the entire thing perfectly and outputs everything I need it to.
Any ideas what I have done wrong?
function FinalGrade
FinalGrade = FinalMark;
PartBAvg = (sum(Results{1:6,7})/6);
PartCAvg = (sum(Results{7:18,7})/12);
FinalMark = ((PartBAvg*0.33)+(PartCAvg*0.66))
if (FinalMark >= 0) && (FinalMark <= 100)
disp ('Check Complete, all Results are within reasonable parameters')
else
disp ('Error in calculation. Check Data source')
end
if (FinalMark >= 70)
disp ('First Class Degree')
elseif (60 <= FinalMark) && (FinalMark < 70)
disp ('Upper Second Class Degree')
elseif (50 <= FinalMark) && (FinalMark < 60)
disp ('Lower Second Class Degree')
elseif (40<= FinalMark) && (FinalMark < 50)
disp ('Third Class Degree')
else
disp ('Fail')
end

 Risposta accettata

%FinalGrade = FinalMark;

7 Commenti

Comment the above line nd run the function. The name of the function is assigned to a variable FinalMark
I had tried that previously, it still fails to return any of the Disp values from inside the function. Disp Values outside of it are still displayed though
Also, there are no inputs to function FinalGrade function, which are being used inside the function.
function FinalGrade(FinalMark, Results)
@Thomas Brown, you might want to return the values FinalMark as the function output as below
Results = randn(18,7); % input to the function
FinalMark = FinalGrade(Results)
FinalMark = 0.3367
Check Complete, all Results are within reasonable parameters Fail
FinalMark = 0.3367
function FinalMark = FinalGrade(Results)
PartBAvg = (sum(Results(1:6,7))/6);
PartCAvg = (sum(Results(7:18,7))/12);
FinalMark = ((PartBAvg*0.33)+(PartCAvg*0.66))
if (FinalMark >= 0) && (FinalMark <= 100)
disp ('Check Complete, all Results are within reasonable parameters')
else
disp ('Error in calculation. Check Data source')
end
if (FinalMark >= 70)
disp ('First Class Degree')
elseif (60 <= FinalMark) && (FinalMark < 70)
disp ('Upper Second Class Degree')
elseif (50 <= FinalMark) && (FinalMark < 60)
disp ('Lower Second Class Degree')
elseif (40<= FinalMark) && (FinalMark < 50)
disp ('Third Class Degree')
else
disp ('Fail')
end
end
That didn't seem to work either. I will include below the entire script and not just the function. This may help slightly understand the problem.
The results tabloe, Module Mark and Word Check all return values in the command window. Nothing inside the function does. If the 'Check complete, all results are within....') if statement is placed prior to the function then it will display the correct statement. Inside the function it displays nothing.
Results = readtable("JansData.xlsx");
Results = renamevars (Results, "Var1", "Module");
Results = renamevars (Results, "Var2", "CW_Mark");
Results = renamevars (Results, "Var3", "CW_Weight");
Results = renamevars (Results, "Var4", "Exam_Mark");
Results = renamevars (Results, "Var5", "Exam_Weight");
Results = renamevars (Results, "Var6", "Credits");
Results.FinalMark = (Results.CW_Mark.*(Results.CW_Weight/100) ...
+Results.Exam_Mark.*(Results.Exam_Weight/100))
WordCheck = find(strcmp(Results.CW_Weight,'NaN'))
if WordCheck ~= 0
disp ('Words Found within Dataset')
else
disp('No Words discovered')
end
ModuleMark = Results{5,7}
PartBAvg = (sum(Results{1:6,7})/6);
PartCAvg = (sum(Results{7:18,7})/12);
if PartBAvg == 'NaN'
disp ('Error, text discovered in Datasets')
elseif PartCAvg == 'NaN'
disp ('Error, text discovered in Datasets')
else
end
function FinalGrade
% FinalGrade = FinalMark;
PartBAvg = (sum(Results{1:6,7})/6);
PartCAvg = (sum(Results{7:18,7})/12);
FinalMark = ((PartBAvg*0.33)+(PartCAvg*0.66))
if (FinalMark >= 0) && (FinalMark <= 100)
disp ('Check Complete, all Results are within reasonable parameters')
else
disp ('Error in calculation. Check Data source')
end
if (FinalMark >= 70)
disp ('First Class Degree')
elseif (60 <= FinalMark) && (FinalMark < 70)
disp ('Upper Second Class Degree')
elseif (50 <= FinalMark) && (FinalMark < 60)
disp ('Lower Second Class Degree')
elseif (40<= FinalMark) && (FinalMark < 50)
disp ('Third Class Degree')
else
disp ('Fail')
end
end
@Thomas Brown, thats because you are not passing any input to the function FinaGrade. See the below format of the function.
function FinalMark = FinalGrade(Results)
% <-----> input variable data
end
Also note i have used Results as numeric array whereas you have cell arrays since you import data using readtable. So change the ( ) to { } for Results inside the function
That has now worked. Thank you for all your help

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by