Trying to use a user-define function but keep getting ERROR: "Unrecognized function or variable 'tolerance'."

2 次查看(过去 30 天)
L = data(:,3); %this is the length/value of each item
for k = 1:length(P)
item.test(k) = tolerance(item.cat(k),L);
end
%%
%functions
function [fit] = tolerance(category,value)
if category == 1
if value>=25.1 && value<=26.05
fit = 55;
else
fit = 99;
end
elseif category == 2
if value>=25.92 && value<=26.08
fit = 55;
else
fit = 99;
end
elseif category == 3
if value>=25.88 && value<=26.12
fit = 55;
else
fit = 99;
end
else
if value>=25.85 && value<=26.15
fit = 55;
else
fit = 99;
end
end
end
I'm trying to use a user defined function in a for loop but keep getting the error
"Unrecognized function or variable 'tolerance'." and I'm unsure whats wrong.
I have a strcuture field called (item.cat) which is the category of 4 items (1-4) and the user defined function I made is supposed to go through all of the values in the structure field and determine if each item fits the specific tolerences based on its category.
I've tried to look it up and ask a friend for help but couldn't figure it out.
  3 个评论
Walter Roberson
Walter Roberson 2020-4-29
编辑:Walter Roberson 2020-4-29
MATLAB does not require functions to have their own .m file in all cases. There are three important exceptions:
  • For decades, multiple functions can be stored inside the same .m file. They will effectively be private to that file unless the code deliberately stores a function handle to them.
  • Since R2008a, multiple functions can be stored inside a class definition classdef . The class definition can control the visibility of the functions
  • In R2016b and later, functions can appear at the end of script .m files, provided that the function name is not the same as the script name. They will effectively be private to that file unless the code deliverately stores a function handle to them.

请先登录,再进行评论。

回答(0 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by