‘function’ 在Octave中未定义

huangapple go评论63阅读模式
英文:

'function' undefined in Octave

问题

以下是您要翻译的内容:

"I'm writing a script, and I have two functions. The first one was working fine. Now I added a second one (that is called before the first one) and I get the error stated in title.

I already had the "dummy" (1;) at the beginning of the script to start the file with function declarations.

Here's the code (until the error, it's not complete):"

1;
%%=========插值函数=================
function Y=interpdata(x1,y1,x2,y2,X)
    Y=interp1([x1,x2],[y1,y2],X);
endfunction
%%=====插值函数结束==============

%%=========最小均方回归函数=============
function coef=LMSregression(mat,size_)
    sumX2=0;
    sumXY=0;
    sumX=0;
    sumY=0;
    for i=1:size_
        sumX=sumX+mat(i,1);
        sumY=sumY+mat(i,2);
        sumX2=sumX2+mat(i,1)*mat(i,1);
        sumXY=sumXY+mat(i,1)*mat(i,2);
    endfor
    slope = (size_ * sumXY - sumX * sumY) /(size_ * sumX2 -sumX * sumX);
    b = sumY/size_ - slope * sumX/size_;
    coef = [slope b];
endfunction
%%=======最小均方回归函数结束========

clear all;

%%============校准和分割============
data_ = dlmread ("mono00.txt", ";");
size_data=size(data_);

counter1=1;
counter2=1;
counter3=1;
for i=1:size_data(1)
    if data_(i,1)==1
        zone1(counter1,:)=data_(i,:);
        counter1=counter1+1;
    elseif data_(i,1)==2
        zone2(counter2,:)=data_(i,:);
        counter2=counter2+1;
    elseif data_(i,1)==3
        zone3(counter3,:)=data_(i,:);
        counter3=counter3+1;
    endif
endfor
%%======校准和分割结束===========

%%=================数据清理和排序============
zone1=[zone1(:,2) zone1(:,6) ];                       
zone2=[zone2(:,2) zone2(:,6) ];                       
zone3=[zone3(:,2) zone3(:,6) ];
zone1=sortrows(zone1, 1);
zone2=sortrows(zone2, 1);
zone3=sortrows(zone3, 1);
size_zone1=counter1-1;
size_zone2=counter2-1;
size_zone3=counter3-1;
%%===========数据清理和排序结束===========

%%=================LMS回归==============
coefs=zeros(3,2);
coefs(1,:)=LMSregression(zone1, size_zone1);    <----错误: 'LMSregression' 在第69行附近未定义,列12
X1=linspace(min(zone1(:,1)), max(zone1(:,1)),100);
coefs(2,:)=LMSregression(zone2, size_zone2);
X2=linspace(min(zone2(:,1)), max(zone2(:,1)),100);
coefs(3,:)=LMSregression(zone3, size_zone3);
X3=linspace(min(zone3(:,1)), max(zone3(:,1)),100);
%%=============LMS回归结束==============
英文:

I'm writing a script, and I have two functions. The first one was working fine. Now I added a second one (that is called before the first one) and I get the error stated in title.

I already had the "dummy" (1;) at the beginning of the script to start the file with function declarations.

Here's the code (until the error, it's not complete):

1;
%%=========INTERPOLATION FUNCTION=================
function Y=interpdata(x1,y1,x2,y2,X)
Y=interp1([x1,x2],[y1,y2],X);
endfunction
%%=====end of INTERPOLATION FUNCTION==============
%%=========LEAST MEAN SQUARE FUNCTION=============
function coef=LMSregression(mat,size_)
sumX2=0;
sumXY=0;
sumX=0;
sumY=0;
for i=1:size_
sumX=sumX+mat(i,1);
sumY=sumY+mat(i,2);
sumX2=sumX2+mat(i,1)*mat(i,1);
sumXY=sumXY+mat(i,1)*mat(i,2);
endfor
slope = (size_ * sumXY - sumX * sumY) /(size_ * sumX2 -sumX * sumX);
b = sumY/size_ - slope * sumX/size_;
coef = [slope b];
endfunction
%%=======end of LEAST MEAN SQUARE FUNCTION========
clear all;
%%============CALIBRATION AND DIVISION============
data_ = dlmread ("mono00.txt", ";");
size_data=size(data_);
counter1=1;
counter2=1;
counter3=1;
for i=1:size_data(1)
if data_(i,1)==1
zone1(counter1,:)=data_(i,:);
counter1=counter1+1;
elseif data_(i,1)==2
zone2(counter2,:)=data_(i,:);
counter2=counter2+1;
elseif data_(i,1)==3
zone3(counter3,:)=data_(i,:);
counter3=counter3+1;
endif
endfor
%%======end of CALIBRATION AND DIVISION===========
%%=================DATA CLEAN AND SORT============
zone1=[zone1(:,2) zone1(:,6) ];                       
zone2=[zone2(:,2) zone2(:,6) ];                       
zone3=[zone3(:,2) zone3(:,6) ];
zone1=sortrows(zone1, 1);
zone2=sortrows(zone2, 1);
zone3=sortrows(zone3, 1);
size_zone1=counter1-1;
size_zone2=counter2-1;
size_zone3=counter3-1;
%%===========end of DATA CLEAN AND SORT===========
%%=================LMS REGRESSION=================
coefs=zeros(3,2);
coefs(1,:)=LMSregression(zone1, size_zone1);    <----error: 'LMSregression' undefined near line 69, column 12
X1=linspace(min(zone1(:,1)), max(zone1(:,1)),100);
coefs(2,:)=LMSregression(zone2, size_zone2);
X2=linspace(min(zone2(:,1)), max(zone2(:,1)),100);
coefs(3,:)=LMSregression(zone3, size_zone3);
X3=linspace(min(zone3(:,1)), max(zone3(:,1)),100);
%%=============end of LMS REGRESSION==============

It's probably a minor bug, but I can't find anything anywhere. Every post just talks about the need for a dummy, and that's not it in this case.

答案1

得分: 1

就像用户@PierU在评论中所说:问题在于clear all也会清除函数。

多位用户表示应该使用clear代替。

英文:

Like the user @PierU stated in the comments: the problem is that the clear all clears the functions as well.

Multiple users stated that clear should be used instead.

huangapple
  • 本文由 发表于 2023年7月6日 15:15:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定