简体   繁体   English

Octave从另一个m文件调用函数

[英]Octave calling function from another m file

m file that calls function nraizes(a) from another m file. 调用函数的m文件从另一个m文件中nraizes(a)

clear functions;
clc;
x = input('Insert value for a? ') ;
% call to nraizes()
w = nraizes(x)
clear functions;

nraizes.m file with nraizes() function: 带有nraizes()函数的nraizes.m文件:

printf("\n\n");
printf("nraizes por André Castro - UAB 901396");
printf("\n");
printf("Usar na próxima prompt: nraizes(valor numérico)");
printf("\n");

function n = nraizes(a)

% limpar a memoria de todas as vars e funções
clear functions;

clc;

% intervalo para x
x = 0:.1:25;
% ambas as funções h(x) e g(x)
h = @(x) cos(x);
g = @(x) exp(a*x)-1;
% traçar linha na origem das abcissas
or = x;
or(:) = 0;
% gráfico
plot(x, [h(x);g(x);or]);
axis([0, 25, -1, 1])
title("h(x),g(x)");
grid on;
printf("Fim do Script");
printf("\n");

% limpar a memoria de todas as vars e funções
clear functions;

endfunction

It always throws the following error: 它总是抛出以下错误:

warning: function 'nraizes' defined within script file '/Users/andrecastro/0_Thawed/1_UAB/1 UCS_INFORMATICA/Computacao Numerica/0 2015-2016_CN/1 Algoritmos_Octave/nraizes.m'
error: invalid use of script /Users/andrecastro/0_Thawed/1_UAB/1 UCS_INFORMATICA/Computacao Numerica/0 2015-2016_CN/1 Algoritmos_Octave/nraizes.m in index expression
error: called from:
error:   /Users/andrecastro/0_Thawed/1_UAB/1 UCS_INFORMATICA/Computacao Numerica/0 2015-2016_CN/1 Algoritmos_Octave/script.m at line 19, column 7

I don't undestand why. 我不明白为什么。 Both .m files are in same path. 两个.m文件都在同一个路径中。

You have nraizes defined in a file with the same name as the function. 您已在与该函数同名的文件中定义了nraizes You should not do it because now you have both a script and a function, both named nraizes which makes things confusing. 你不应该这样做,因为现在你同时拥有一个脚本和一个函数,这两个命名的nraizes让事情变得混乱。

This is what the first warning means: 这是第一个警告意味着:

warning: function 'nraizes' defined within script file '[...]/nraizes.m'

It is warning that the function is on a script with the same name. 警告该函数在具有相同名称的脚本上。

You can do it (it's only a warning) but you shouldn't. 你可以这样做(这只是一个警告)但你不应该这样做。 If you want to do it, you will need to first source the nraizes script and only after that will you have the function available: 如果你想这样做,你需要首先获取nraizes脚本,然后才能使用该函数:

nraizes; # source nraizes script
w = nraizes (x); # call nraizes function

That is why you got an error 这就是你遇到错误的原因

error: invalid use of script [...]nraizes.m in index expression

because you tried to call a function (or index a variable -- remember that the function is not defined yet so Octave does not know) that does not exist. 因为你试图调用一个函数(或索引变量 - 记住函数尚未定义,因此Octave不知道)不存在。

However, the printf statements at the top of nraizes.m suggest that you actually want to have it as a function file only. 但是, nraizes.m顶部的printf语句表明您实际上只想将其作为函数文件。 In that case, you should drop those printf 's (replace them with comments and they will be displayed when you run help nraizes ) so that the first statement is the actual function definition. 在这种情况下,你应该删除那些printf (用注释替换它们,并在运行help nraizes时显示它们),这样第一个语句就是实际的函数定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM