Modified Thompson - User Defined Function Errors - "function 'modifiedthompson' has already been declared within this scope", "the function return value might be unset", and "the function 'modifiedthompson' might be unused".

10 views (last 30 days)
Hello, I'm trying to get my user defined function to work when I call it out, but I seem to keep on running into the error of, "function 'modifiedthompson' has already been declared within this scope", "the function return value might be unset", and "the function 'modifiedthompson' might be unused".
In the first Script, named Lab_6 I realized my data was not going through the function because evidently I don't know the full scope of how to use user defined functions, so I tried to run the data through by calling it out (can be seen in the Lab_6_OutlierRemoval), but I seem to keep on running into issues. I will also copy the code from the script, modifiedthompson that I am trying to make into a user defined function that can be called out in the script, Lab_6_OutlierRemoval. I apologize about the length of this, but I've been trying to figure this out for forever. Skip to end to see the user defined function where I call the file name modifiedthompson.
--------------------------------------------------------------------------------------------------------------------------------------
Lab_6
clear,clc;
%Tau method
%attempting to figure out how to loop all data throughfor i=1
%precision uncertainty
Q = ([0.00,.2697,.3543,.4081])
V = ([0,21.75,28.57,32.91])
a1=(V*Q')/(V*V'); %Rosemount - Q-values & RTDs are V-values
e=a1*V-Q;
N=length(Q);
Sxy=sqrt(e*e'/(N-2));
nu=N-1;
P=0.95;
tnup=sqrt(nu/betaincinv(1-P,nu/2,1/2)-nu);
Dp=tnup*Sxy/sqrt(N)
%Calibration Accuracy
Sa0=Sxy*sqrt(V*V'/(N*(V*V')-sum(V)^2));
Dz=tnup*Sa0/sqrt(N)
%Sensitivity error
Sal=Sxy*sqrt(N/(N*(V*V')-sum(V)^2));
ro=max(Q);
rx=ro/a1;
Ds=tnup*Sal*rx/sqrt(N)
%accuracy error
Da=sqrt((Dz^2)+(Ds^2))
%Total Calibration error
Dc=sqrt((Da^2)+(Dp^2))
%Temperature Coefficient using Eq. 3 from Lab Manual
Vs=14;
V0=5.7691;
G=21.9;
alpha = (4/(a1*Vs*G))*V0
%Tau method
%Select specific column data=xlsread('file.xlsx','C:C')
data = xlsread('room_temp.xlsx','A:A')
display(mean(data))
function [outlier_removal] = modifiedthompson(data)
runs = 0;
while 1
N=length(data);
nu=N-2;
Mx=mean(data);
Sx=std(data);
P=0.95;
tnup = sqrt(nu/betaincinv(1-P,nu/2,1/2)-nu);
tau = (nu+1)*tnup/sqrt(N*(nu+tnup^2));
xL=Mx-tau*Sx;
xU=Mx+tau*Sx;
newdata = data(data < xU & data > xL);
runs = runs + 1;
if length(newdata)==length(data)
break
else
data = newdata;
end
end
outlier_removal = data;
end
--------------------------------------------------------------------------------------------------------------------------------------
Lab_6_OutlierRemoval
clear,clc;
%Tau method
%attempting to figure out how to loop all data throughfor i=1
%precision uncertainty
Q = ([0.00,.2697,.3543,.4081])
V = ([0,21.75,28.57,32.91])
a1=(V*Q')/(V*V'); %Rosemount - Q-values & RTDs are V-values
e=a1*V-Q;
N=length(Q);
Sxy=sqrt(e*e'/(N-2));
nu=N-1;
P=0.95;
tnup=sqrt(nu/betaincinv(1-P,nu/2,1/2)-nu);
Dp=tnup*Sxy/sqrt(N)
%Calibration Accuracy
Sa0=Sxy*sqrt(V*V'/(N*(V*V')-sum(V)^2));
Dz=tnup*Sa0/sqrt(N)
%Sensitivity error
Sal=Sxy*sqrt(N/(N*(V*V')-sum(V)^2));
ro=max(Q);
rx=ro/a1;
Ds=tnup*Sal*rx/sqrt(N)
%accuracy error
Da=sqrt((Dz^2)+(Ds^2))
%Total Calibration error
Dc=sqrt((Da^2)+(Dp^2));
%Temperature Coefficient using Eq. 3 from Lab Manual
Vs=14;
V0=5.7691;
G=21.9;
alpha = (4/(a1*Vs*G))*V0
%Tau method
%Select specific column data=xlsread('file.xlsx','C:C')
data = xlsread('room_temp.xlsx','A:A');
%Outlier Removal w/ User Defined Function
outlier_removal = modifiedthompson(data);
display(outlier_removal)
--------------------------------------------------------------------------------------------------------------------------------------
modifiedthompson
clear,clc;
%Tau method
%Select specific column data=xlsread('file.xlsx','C:C')
data = xlsread('room_temp.xlsx','A:A')
display(mean(data))
function [outlier_removal] = modifiedthompson(data)
runs = 0;
while 1
N=length(data);
nu=N-2;
Mx=mean(data);
Sx=std(data);
P=0.95;
tnup = sqrt(nu/betaincinv(1-P,nu/2,1/2)-nu);
tau = (nu+1)*tnup/sqrt(N*(nu+tnup^2));
xL=Mx-tau*Sx;
newdata = data(data < xU & data > xL);
runs = runs + 1;
if length(newdata)==length(data)
break
else
data = newdata;
end
end
end

Answers (1)

DGM
DGM on 7 Apr 2021
Edited: DGM on 7 Apr 2021
I'm not sure about what you're showing here. I see three blocks of code. As I understand it, the first is your script with a local function modifiedthompson(). The other two blocks were an attempt to make the function external. Is that right?
If so, there are some basic issues. I don't know what any of this code is supposed to do, but let's just try to fix things that are obviously suspect. Let's start with the third file:
% --------------------------------------------------------------------------------------------------------------------------------------
% modifiedthompson
% the function definition should be the first non-comment line in the file
% since you're passing in the data as an argument, you don't need to try to
% read it again.
function [outlier_removal] = modifiedthompson(data)
% if there are other things you want to do before the loop,
% they'd go here inside the function scope
runs = 0;
while 1
N=length(data);
nu=N-2;
Mx=mean(data);
Sx=std(data);
P=0.95;
tnup = sqrt(nu/betaincinv(1-P,nu/2,1/2)-nu);
tau = (nu+1)*tnup/sqrt(N*(nu+tnup^2));
xL=Mx-tau*Sx;
xU=Mx+tau*Sx; % this line was missing?
newdata = data(data < xU & data > xL);
runs = runs + 1;
if length(newdata)==length(data)
break
else
data = newdata;
end
end
% we've exited the loop, but we still hadn't assigned anything to
% the output outlier_removal.
outlier_removal = data;
end
If i save this file as modifiedthompson.m, I can call it with a dummy input
a = modifiedthompson(1:10)
and I get output. I don't know if it's correct, but the function runs without error. File 2 will now run and call the external function.
Looking at File 1 with the local function, the issue appears that the function isn't being called.
clear,clc;
%Tau method
%attempting to figure out how to loop all data throughfor i=1
%precision uncertainty
Q = ([0.00,.2697,.3543,.4081])
V = ([0,21.75,28.57,32.91])
a1=(V*Q')/(V*V'); %Rosemount - Q-values & RTDs are V-values
e=a1*V-Q;
N=length(Q);
Sxy=sqrt(e*e'/(N-2));
nu=N-1;
P=0.95;
tnup=sqrt(nu/betaincinv(1-P,nu/2,1/2)-nu);
Dp=tnup*Sxy/sqrt(N)
%Calibration Accuracy
Sa0=Sxy*sqrt(V*V'/(N*(V*V')-sum(V)^2));
Dz=tnup*Sa0/sqrt(N)
%Sensitivity error
Sal=Sxy*sqrt(N/(N*(V*V')-sum(V)^2));
ro=max(Q);
rx=ro/a1;
Ds=tnup*Sal*rx/sqrt(N)
%accuracy error
Da=sqrt((Dz^2)+(Ds^2))
%Total Calibration error
Dc=sqrt((Da^2)+(Dp^2))
%Temperature Coefficient using Eq. 3 from Lab Manual
Vs=14;
V0=5.7691;
G=21.9;
alpha = (4/(a1*Vs*G))*V0
%Tau method
%Select specific column data=xlsread('file.xlsx','C:C')
%data = xlsread('room_temp.xlsx','A:A')
data=1:10; % i don't have your file, so here's a dummy vector.
% actually need to call the function
%Outlier Removal w/ User Defined Function
outlier_removal = modifiedthompson(data);
% do you mean to be looking mean(data)
% or mean(outlier_removal)?
display(mean(data))
function [outlier_removal] = modifiedthompson(data)
runs = 0;
while 1
N=length(data);
nu=N-2;
Mx=mean(data);
Sx=std(data);
P=0.95;
tnup = sqrt(nu/betaincinv(1-P,nu/2,1/2)-nu);
tau = (nu+1)*tnup/sqrt(N*(nu+tnup^2));
xL=Mx-tau*Sx;
xU=Mx+tau*Sx;
newdata = data(data < xU & data > xL);
runs = runs + 1;
if length(newdata)==length(data)
break
else
data = newdata;
end
end
outlier_removal = data;
end
Maybe that's a start
  2 Comments
Adam Treptau
Adam Treptau on 8 Apr 2021
Yes, thank you so much! I'll see what I can get out of this. To answer your question though, The bottom code is an attempt to create a user defined function that can be used for various sets of data where I will be able to call the file name (or function) in another script, so I can use it for different data sets or calculations in the future. It's intended use is to remove "outliers" in data sets through the Thompson Tau method.
How to do the thompson tau method is all of the lines of code in the modifiedthompson code after the while loop. The while loops is to run it the data through continuously until all of the outliers are removed.
I'm not sure what you mean by local function or external, but I might have an idea of what you mean, and I hope I answered that in the first paragraph. The top two codes were my attempt to use the modifiedthompson.m file to run the data set through the function named outlier_removal... I hope this cleared everything up! I wish there was a way to do a video call that would probably clear up any confusion.
DGM
DGM on 8 Apr 2021
The only (user-defined) function in any of these is modifiedthompson(). outlier_removal is a variable.
When I say local or external function, I'm just referring to where the function is physically defined. If I say external, I mean the function is in a separate file and can be called from other files. If it's local, it shares the same file as a script or function, and is only accessible within the scope of that file.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!