Adding two functions to 1 m-file, while making variables in the two functions accessible to both?

1 次查看(过去 30 天)
Hello,
I have 2 Matlab functions that I want to combine into 1 m-file. The first function "F1" needs data from the second function "F2". The second function uses "F1" to perform the calculations. How do I combine F1 and F2 such that variables in both functions are accessible/recognized by the two functions? I don't want to redefine variables that are needed by F2, when they are already there in F1.
The two functions are something like this: %The first function 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
%The second function function [ ]=F2
(1) A1 and A2 imported
(2) for loop that calls F1 to perform the calculations end
Note: I might need to utilize F1, but not F2 in other functions later on. F2 is only needed by F1.
Any suggestions on how to go about this would be appreciated. Thanks.

回答(1 个)

Thorsten
Thorsten 2015-6-2
编辑:Thorsten 2015-6-2
Define function F1 and use the matrices read by F2 as the default arguments.
function Y = F1(A1,A2)
if isempty(A1) && isempty(A2)
% import A1 and A2 from file
% put body of function F2 here
end
% computations on A1 and A2 defined in such a way that they work on each element if A1 and A2 are matrices

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by