Trying to use a user-define function but keep getting ERROR: "Unrecognized function or variable 'tolerance'."
Show older comments
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 Comments
Upon second reading: matlab requires functions to have their own file.
cut the part from line
'function [fit] = tolerance(category,value)' to the end of the file, and save it in it's own file called 'tolerance.m'. it's important that the name of the file and the function name within it match.
------------------------
it's likely that your current working directory doesn't include 'tolerance.m' and that the directory that it IS in is not in the current path.
if the file is e.g. '/home/jessical/matlab_functions/tolerance.m', you could try:
addpath('/home/jessical/matlab_functions')
or whatever. then it should work
Jessica Larry
on 29 Apr 2020
Walter Roberson
on 29 Apr 2020
Edited: Walter Roberson
on 29 Apr 2020
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.
Answers (0)
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!