Matlab中的“未定义函数或变量”

时间:2022-10-10 19:40:55

Here is my code:

这是我的代码:

    function [im,sindx,end1]=alln(im,i,j,secret,sindx,end1)
    slen=length(secret);
    p=im(i,j);
    neigh= [im(i-1,j) im(i+1,j) im(i,j-1) im(i,j+1) im(i-1,j-1) im(i+1,j-1) im(i-1,j+1) im(i+1,j+1)];
    minpix = min (neigh)
    maxpix = max (neigh)

        if minpix < p < maxpix
        lowlim = minpix+1;
        highlim = maxpix-1;   
        range = highlim-lowlim+1;

        nbits=floor(log2(abs(range)));   

        if sindx+nbits-1>slen
            end1=1;
            return
        end
        for k=1:nbits
            bin(k)=secret(sindx+k-1);
        end
        b = bin2dec(bin);
        newvalue1 = abs (minpix + b);
        newvalue2 = abs (maxpix - b);
        if abs(p-newvalue1)<= abs(p-newvalue2)
            im(i,j) = newvalue1;
        else
            im(i,j) = newvalue2;
        end

        sindx=sindx+nbits;

    end
end

My main program calls this function. When I run the program, I get the following error message:

我的主程序调用这个函数。当我运行程序时,我得到以下错误消息:

??? Undefined function or variable "bin".

Error in ==> alln at 34
            b = bin2dec(bin);

I know there are many experts for whom this is not a problem at all. I am new to MATLAB. Please guys, show me the way, which type of modification in the code can overcome this problem?

我知道有很多专家认为这根本不是问题。我是MATLAB新手。伙计们,给我示范一下,代码中哪一种修改可以解决这个问题?

2 个解决方案

#1


1  

First of all, are there some lines missing from the file? Perhaps you've stripped some comments from the top? Because the error message says that

首先,文件中是否缺少一些行?也许你已经从上面删除了一些评论?因为错误信息是这么说的

b = bin2dec(bin); 

is line 34, but it's line 22 in the code you present.

是第34行,但是在代码中是第22行。

OK, that aside...

好的,那一边……

The error message says that 'bin' isn't defined, but I see that it's being set on the line...

错误消息说“bin”没有定义,但是我看到它被设置在这行……

bin(k)=secret(sindx+k-1); 

That suggests to me that THAT line isn't being run.

这对我来说意味着这条线没有运行。

I see that that bin = ... line is inside of a 'for' loop, so I suspect that the for loop is run zero times, meaning that 'bin' never gets defined. What is nbits? Is it 1, or perhaps less than 1? THAT would prevent the loop from running at all.

我看到那个箱子=……line位于“for”循环的内部,因此我怀疑for循环的运行次数为零,这意味着“bin”永远不会被定义。nbits是什么?是1,还是小于1?这将完全阻止循环运行。

Try removing the semicolon from the end of the

尝试删除分号的末尾

nbits=floor(log2(abs(range))); 

line and run your code again.

行并再次运行代码。

Leaving off the semicolon will force the value of nbits to be printed in the Command Window. I bet you'll find that it's 1 or less. If that's the case, then start looking at HOW nbits is calculated, and I bet you'll find the problem.

去掉分号将强制在命令窗口中打印nbits的值。我打赌你会发现它是1或更小。如果是这样,那么开始看看如何计算nbits,我打赌你会发现问题。

#2


0  

At what input arguments to the function alln, are you getting the error?

在函数alln的输入参数中,你得到误差了吗?

Lets suppose that nbits is 0, then the following loop will not run:

假设nbits是0,那么下面的循环不会运行:

for k=1:nbits
    bin(k)=secret(sindx+k-1);
end

So, bin will be undefined. So, the error happens. This is one of the cases where the error can happen. There are many such possible cases.

所以,bin是没有定义的。所以,错误发生。这是可能发生错误的情况之一。有很多这样的可能案例。

#1


1  

First of all, are there some lines missing from the file? Perhaps you've stripped some comments from the top? Because the error message says that

首先,文件中是否缺少一些行?也许你已经从上面删除了一些评论?因为错误信息是这么说的

b = bin2dec(bin); 

is line 34, but it's line 22 in the code you present.

是第34行,但是在代码中是第22行。

OK, that aside...

好的,那一边……

The error message says that 'bin' isn't defined, but I see that it's being set on the line...

错误消息说“bin”没有定义,但是我看到它被设置在这行……

bin(k)=secret(sindx+k-1); 

That suggests to me that THAT line isn't being run.

这对我来说意味着这条线没有运行。

I see that that bin = ... line is inside of a 'for' loop, so I suspect that the for loop is run zero times, meaning that 'bin' never gets defined. What is nbits? Is it 1, or perhaps less than 1? THAT would prevent the loop from running at all.

我看到那个箱子=……line位于“for”循环的内部,因此我怀疑for循环的运行次数为零,这意味着“bin”永远不会被定义。nbits是什么?是1,还是小于1?这将完全阻止循环运行。

Try removing the semicolon from the end of the

尝试删除分号的末尾

nbits=floor(log2(abs(range))); 

line and run your code again.

行并再次运行代码。

Leaving off the semicolon will force the value of nbits to be printed in the Command Window. I bet you'll find that it's 1 or less. If that's the case, then start looking at HOW nbits is calculated, and I bet you'll find the problem.

去掉分号将强制在命令窗口中打印nbits的值。我打赌你会发现它是1或更小。如果是这样,那么开始看看如何计算nbits,我打赌你会发现问题。

#2


0  

At what input arguments to the function alln, are you getting the error?

在函数alln的输入参数中,你得到误差了吗?

Lets suppose that nbits is 0, then the following loop will not run:

假设nbits是0,那么下面的循环不会运行:

for k=1:nbits
    bin(k)=secret(sindx+k-1);
end

So, bin will be undefined. So, the error happens. This is one of the cases where the error can happen. There are many such possible cases.

所以,bin是没有定义的。所以,错误发生。这是可能发生错误的情况之一。有很多这样的可能案例。