Matlab-无法调用函数,因为它表示我正在尝试执行脚本 [英] Matlab - Can't call function because it says I'm trying to execute a script

查看:303
本文介绍了Matlab-无法调用函数,因为它表示我正在尝试执行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中,我定义了一个名为iReadImage的函数,它看起来像:

In Matlab I defined a function called iReadImage it looks like:

function [outimag] = iReadImage(imaurl)

function [outimag] = iReadImage(imaurl)

{code}

我用了好几个小时,一切似乎都正常,但是后来我换了一行,突然之间,即使我删除了那行,也没有任何作用.它总是告诉我:

I used it for hours and everything seemed to work fine but then I changed one line and all of a sudden it didn't work anymore, even after I deleted that line nothing worked. It always tells me:

尝试将SCRIPT iReadImage作为函数执行:

Attempt to execute SCRIPT iReadImage as a function:

/home/.../iReadImage.m

/home/.../iReadImage.m

当我查看文件时说它是0kB....不知道为什么,我尝试了所有事情,将功能复制到新功能,重新启动计算机,甚至在其他PC上也尝试过.它似乎可以再次工作两次或三遍,但是直到我得到相同的错误消息后,它才持续了很长时间.

When I look at the file it says that it is 0kB....No idea why, I tried kind of everything, copied the function to a new function, rebooted my computer even tried it on other PCs. Two or three times it seemed to work again but never for long until I got the same error message.

推荐答案

Matlab关于如何构造其功能非常特别.文件functionname.m应该以第一行函数[output] = functionname(input)开头.否则,将假定它正在处理脚本而不是函数.另外,如果文件是函数,则可以在其中声明,例如:

Matlab is very particular about how its functions can be constructed. The file functionname.m should start with the first line function [output] = functionname(input). Otherwise, it will assume that it is dealing with a script and not a function. Additionally, if your file is a function, you can declare within it, like:

function y = f(x)

    y = g(x) + 2;   

    function z = g(x)
    z = x.^2;
    end

end

但是,如果您的文件是脚本,则Matlab不允许此类函数声明.一种测试方法是将现有脚本简单地转换为函数(通过将其包装为具有空输入和输出的函数),然后查看是否发生相同的错误.

However, if your file is a script, Matlab does not allow such function declarations. One way to test this would be to trivially turn your existing script into a function (by wrapping it with a function with null input and output), and see if he same error occurs.

这篇关于Matlab-无法调用函数,因为它表示我正在尝试执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆