Clear Filters
Clear Filters

Unrecognized function or variable " " , when I use my function

57 views (last 30 days)
Hi,
I write some function that will make a fit for my data. this is my function:
function Xmax = fit_function(x,y)
X1 = x(x>0.5 & x<2);
Y1 = y(x>0.5 & x<2);
% ymax calculated analytically with Wolfram Mathematica
ymax = @(b) (2-b(2)/b(1))*(2*b(1)/b(2)-1)^(-b(2)/2/b(1));
modelfun = @(b,x) b(3)/ymax(b)*exp((x-b(4))*(b(1)-b(2))).*sech(b(1)*(x-b(4)));
bguess=[78, 10, peaks(1),x(t_peaks(1))];
% [alpha, beta, amplitude, x offset]
% 78 and 10 were calculated by cftool (after given there a guess of 80,20)
beta = nlinfit(X1, Y1, modelfun, bguess);
fittedCurve = modelfun(beta, X1);
Xmax=log(-1+(2*beta(1)/beta(2)));
when I apply this function on some data, I have a problem in the line of bguess.
The function is able to read the "peaks(1)" variable, but not the t_peaks(1) variable. both of them save in the workspace, and slao both of them is double file.
as I say in the summary, the error is:
>> Xmax=fit_function(x,y);
Unrecognized function or variable 't_peaks'.
Error in fit_function (line 12)
bguess=[78, 10, peaks(1), x(t_peaks(1))];
why is append?
It's strange that it can read one of the variables and not the other..
Thanks!(:

Accepted Answer

Shubham
Shubham on 16 May 2024
Hi Yohay,
The issue you're encountering stems from the scope of variables in MATLAB functions. When you define a function in MATLAB, it has its own local scope, meaning it only has access to the variables that are passed into it as arguments or those defined within it. It does not automatically have access to the variables in the MATLAB workspace unless explicitly passed as arguments or accessed in a different manner.
In the line that's causing the error:
bguess=[78, 10, peaks(1), x(t_peaks(1))];
You mentioned that peaks(1) is accessible, but t_peaks(1) is not, leading to an "Unrecognized function or variable" error. This discrepancy likely arises from how these variables are being handled or accessed within your function or script.
Here are a few possible explanations and solutions:
1. peaks is a Function, Not a Variable from the Workspace
If peaks is not explicitly passed to your function but is still accessible, it's likely because MATLAB includes a function named peaks. This function generates a 3D surface and, when called with an argument like peaks(1), it returns a matrix. This is why MATLAB does not throw an error for peaks.
2. Passing t_peaks as an Argument
To fix the issue with t_peaks, you should modify your function to accept it as an argument. Adjust the function definition to include t_peaks:
function Xmax = fit_function(x, y, peaks, t_peaks)
And when you call fit_function, pass the peaks and t_peaks variables:
Xmax = fit_function(x, y, peaks, t_peaks);
3. Global Variables (Not Recommended)
Another workaround, though generally not recommended due to potential for confusion and errors, is to declare t_peaks as a global variable in both your script and function. However, this approach can lead to code that's harder to debug and maintain, so prefer passing variables as arguments.
4. Nested Functions or Anonymous Functions
If t_peaks is used exclusively within a specific context or calculation, you might also consider using nested functions or an anonymous function within your script, if structuring your code this way makes sense for your application. This approach can keep related calculations and variables together.
I hope this helps!

More Answers (1)

Adam Danz
Adam Danz on 16 May 2024
The variable t_peaks is not defined in the fit_function. Neither is the peaks variable but this is not throwing an error because peaks is also a MATLAB function.
To fix this, you should define peaks and t_peaks in the function or pass those values in as input arguments.
If it isn't clear why this is an issue, suppose I ask you what c equals in the c=b+9. You can't answer the question without knowing what b is. MATLAB needs to know what t_peaks. MATLAB also needs to know that peaks is a variable and not a function. By setting the value for peaks or passing it in as an input argument, MATLAB will know that it's a variable and it won't call the peaks() function.
MATLAB's peaks function with an input of 1:
t = peaks(1)
t = 4.1030e-05

Community Treasure Hunt

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

Start Hunting!