- the call is to file1() and the function being called is the very first function in file1.m and file1.m starts with the keyword function (if the first function defined in file1.m is named something different on its function line then the name on the function line will be ignored and the function will be known by the file name); OR
- somehow file2 has been given a handle to a function defined in file1.m and it invokes the handle; OR
- file1.m is a class constructor and the function being called is a method of the class, and file2 is referring to an object of that class
Function call another function
1 回表示 (過去 30 日間)
古いコメントを表示
I have two functions that have pretty similar sections with some minor differences.
File1 is updated more frequently than file2, so I would like matlab to reference file1 sections on file2 so that file2 would be up to date whenever file1 is updated.
I've created a small example on what I'm trying to explain.
function file1(a,b,c)
%%section 1
if a<3
b=a+c;
else
b=a-c;
end
%%section 2
if a>=0
plot(a,c)
else
plot(c,a)
end
%%section 3
if ismember(a,[0 1 2 4]) && c==true
fprintf('running')
if a==4
msgbox('wrong number')
end
end
I was thinking about creating nested functions but if anyone has other ideas, please let me know.
function file1(a,b,c)
file1a
file1b
file1c
%%section 1
function file1a
if a<3
b=a+c;
else
b=a-c;
end
%%section 2
function file1b
if a>=0
plot(a,c)
else
plot(c,a)
end
%%section 3
function file1c
if ismember(a,[0 1 2 4]) && c==true
fprintf('running')
if a==4
msgbox('wrong number')
end
end
Function File2 is similar to File1 except section 2.
function file2(a,b,c)
%% section 1
file1a
%% section 2
if a>=3
plot(a,c)
else
plot(b,a)
end
%% section 3
file1c
0 件のコメント
回答 (2 件)
Walter Roberson
2022 年 7 月 28 日
file2.m can call a function that is defined in file1.m under the following circumstances:
Nested functions and private (local) functions do not affect this: file2 can reach nested and private functions in file1.m only if it somehow has been given an already-constructed handle to the function.
Steven Lord
2022 年 7 月 29 日
If you only want functions in the directory containing your main file1 and file2 functions to be able to call your helpers, consider making your helpers private functions.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Direct Search についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!