将两个函数添加到1 m文件中,同时使两个函数中的变量都可访问?

时间:2015-06-02 09:24:20

标签: matlab function nested

我有2个Matlab函数,我想将它们组合成1个 m文件。第一个函数F1需要来自第二个函数F2的数据。第二个函数使用F1来执行计算。如何将F1F2组合在一起,使两个函数中的变量都可以被这两个函数访问/识别?

我不想重新定义F2所需的变量,当它们已经存在于F1时。

这两个函数是这样的:

%第一个功能

function [ ] = F1 [....,A1,A2]
(1) calculations based on the data in A1 and A2 
%Two matrices that are imported from a text file into F2
(2) Formulas that use the results in (1)
end

%第二个功能

function [ ] = F2 
(1) A1 and A2 imported 
(2) for loop that calls F1 to perform the calculations end

注意:稍后我可能需要在其他功能中使用F1,而不是F2F2只需要F1

任何关于如何解决这个问题的建议都将受到赞赏。

2 个答案:

答案 0 :(得分:0)

听起来你需要使用nested function

答案 1 :(得分:0)

您可以使用全局变量来确保您不必重新定义变量。 Mathwork做了很好的解释。 http://www.mathworks.com/help/matlab/ref/global.html

function [a3]=F1(a1,a2)
global a1
%something including F2
end

function F2(a3,a1)
global a1 %A change to a1  will also affect it in F1
%something including F1
else