I am trying to create a function but continue to get this error: function score = compute_iss_score(a) ↑ Error: Function d -could someone tell me what I am doing incorrectly?
Show older comments
% Clear command window and workspace
clear all
clc
%The goal of this lab is to create a system which can analyze data and
% asses the severity of a traumatic injury.
% Define the AIS scores in a matrix, a
a = [0 3 4 5 3 0]
% To begin, create a function that will use array data as an input and will
% return the ISS score
function score = compute_iss_score(a)
% To ensure the score is accurate, check that all six AIS scores are
% present and that the values are ranked from [0-5]
if length(a) == 6 && all(a >= 0 & a <= 5);
% Now determine the input variables by limiting the input to the
% greatest three AIS scores in matrix a
max_num = maxk(a,3);
% Follow the ISS formula and square the three greatest values
squared_num = max_num.^2;
% Sum the array to calculate ISS score
score = sum(squared_num);
else
% Display the message that the score is unable to be calculated if the
% conditions were not met
disp("Value(s) are not within the range [0-5]; please try again");
% End the statement
end
% End the function
end
Answers (1)
Walter Roberson
on 23 Feb 2025
0 votes
That code would be an error if the file the script is stored in is named the same thing as the function defined in the script, compute_iss_score.m . When you define a function inside a script, the function name must be different than the script name.
Note that you define the function but you never call the function.
Also: in the case where length(a) == 6 && all(a >= 0 & a <= 5) is false, you are not setting the score variable and you are also not using error() . The code would only work for that case if the function was invoked in a context that did not expect any output variables.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!