首页 > 编程知识 正文

matlab gui界面设计,matlab r2018b安装详细步骤

时间:2023-05-06 15:17:19 阅读:268071 作者:1864

Matlab未定义的变量(Matlab undefined variable)

假设我有以下代码:

[w1, d1]=fit1;

for i = 1:10,

p = w1*d1+i;

C(i,:) = p;

end

[w2, d2]=fit2;

for i = 1:10,

q = w2*d2+i;

D(i,:) = q;

end

凡功能fit1 :

function[w1, d1] = fit1

w1 = rand(1);

d1 = rand(1);

和功能fit2 :

function[w2, d2] = fit2

w2 = w1+0.2;

d2 = d1-0.1;

我得到错误: Undefined function or variable 'w1'.

但是参数w1是在fit2被调用之前定义的,为什么这不起作用?

谢谢!

Say I have the following code:

[w1, d1]=fit1;

for i = 1:10,

p = w1*d1+i;

C(i,:) = p;

end

[w2, d2]=fit2;

for i = 1:10,

q = w2*d2+i;

D(i,:) = q;

end

Where function fit1:

function[w1, d1] = fit1

w1 = rand(1);

d1 = rand(1);

and function fit2:

function[w2, d2] = fit2

w2 = w1+0.2;

d2 = d1-0.1;

I get the error: Undefined function or variable 'w1'.

But the parameter w1 is defined before fit2 is called so why doesn't this work?

Thanks!

原文:https://stackoverflow.com/questions/28206635

更新时间:2020-01-25 19:22

最满意答案

这是因为你的函数fit2不知道变量w1和d1 。 每个函数都有它自己的变量空间,它由输入参数和当然在这个函数中定义的变量组成。 函数fit2没有任何输入,因此它不知道任何变量。 为了让代码正常工作,您应该修改fit2以使其具有2个输入(您在此函数中使用该输入):

function[w2, d2] = fit2(w1,d1)

w2 = w1+0.2;

d2 = d1-0.1;

并用它的输入参数调用它:

[w2, d2]=fit2(w1,d1);

This is because your function fit2 doesn't know variables w1 and d1. Every function has it's own variable space, which consists of input arguments and, of course, variables defined inside this function. Function fit2 doesn't have any inputs, therefore it doesn't know any variable. In order your code to work, you should modify fit2 to have 2 inputs (which you are using inside this function):

function[w2, d2] = fit2(w1,d1)

w2 = w1+0.2;

d2 = d1-0.1;

And call it with it's input arguments:

[w2, d2]=fit2(w1,d1);

2015-01-29

相关问答

正如@excaza所指出的那样,使用str2func生成的函数无法访问不在其工作空间中的变量。 这留下了两个变通方法:您可以使用strrep或regexprep : coeff1变为(1)等替换变量名称的出现。或者您可以将所有系数存储在一个向量中并将它们作为第二个参数传递: F = @(x, coeff) [...

coeff(1)*x(1)*x(4); ...

这仍然会让你处理字符串操作和函数句柄操作。 两者都很昂贵且容易破裂。 由于您的原始问题略有不同,并且特别提到您想要速度,让我建议一种

...

看看这里: 错误报告 您可以尝试类似的东西(运行时): <?php

error_reporting(E_ALL ^ E_NOTICE);

?>

或者更新php.ini: error_reporting = E_ALL & ~E_NOTICE

Take a look here: error-reporting You can try something like that (runtime): <?php

error_reporting(E_ALL ^ E_NOTICE);

?>

Or

...

通过主结构时不要使用任何引号。 只需将结构名称直接传递给函数而不使用''。 when passing the main structure don't use any quotes. just pass the structure name straight to the function without using ''.

我发现为什么它不工作.. vision.OpticalFlow已从最新版本的Matlab中删除,我们建议使用opticalFlowHS而不是使用Horn-Schunck method 更多细节在这里: https : //uk.mathworks.com/help/vision/ref/opticalflow-class.html I found out why its not working.. vision.OpticalFlow has been removed from the lates

...

localfunctions仅在R2013b中引入。 您需要将您的MATLAB版本升级到R2013b或更新版本才能使用它。 Mathworks网站上的所有函数参考文档的底部都有一条注释,说明它的引入版本。 localfunctions was only introduced in R2013b. You will need to update your version of MATLAB to R2013b or newer to be able to use it. At the bottom

...

这是因为你的函数fit2不知道变量w1和d1 。 每个函数都有它自己的变量空间,它由输入参数和当然在这个函数中定义的变量组成。 函数fit2没有任何输入,因此它不知道任何变量。 为了让代码正常工作,您应该修改fit2以使其具有2个输入(您在此函数中使用该输入): function[w2, d2] = fit2(w1,d1)

w2 = w1+0.2;

d2 = d1-0.1;

并用它的输入参数调用它: [w2, d2]=fit2(w1,d1);

This is because your func

...

您可以将g定义为匿名函数 g = @(x)1/2*(1+5/x) 然后以通常的方式调用函数fixedpoint You can define g as a anonymous function g = @(x)1/2*(1+5/x) Then call your function fixedpoint in usual way

盲目地调整工作空间中的值是非常不可取的,并且可能导致难以检测到错误并使代码难以维护。 反对这一点的论据实质上是反映全局变量的参数 。 明确控制您想要操作的数据将更加可靠。 是的,这可能要求您以不同方式存储数据。 例如,您可以使用结构 : function testcode()

mydata.a = 1;

mydata.b = 2;

mydata.c = 3;

mydata.d = 4;

mydata = multiplydata(mydata, 2);

disp(mydata)

end

fun

...

您只需要删除.m扩展名: matlab -nodesktop -nosplash -r "my_script"

原因是my_script.m不是有效的Matlab语句。 要运行脚本/函数,您需要使用其名称(即my_script执行它。 如果您尝试直接从Matlab命令窗口运行my_script.m和my_script语句,则可以看到。 您提到的第二个错误(使用run命令时)似乎是脚本中的实际错误。 您似乎忘记了复制粘贴顶行,该顶行应显示发生错误的行号。 你在下面看到的,即Error in run

...

这些是函数的内容, 逐字 : function setdemorandstream(n)

%SETDEMORANDOMSTREAM Set default stream for reliable example results.

%

% SETDEMORANDOMSTREAM(N) is less distracting in example code, but

% equivalent to:

%

% rs = RandStream('mcg16807','Seed',n);

%

...

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。