Function call another function

4 ビュー (過去 30 日間)
Emily
Emily 2022 年 7 月 28 日
回答済み: Steven Lord 2022 年 7 月 29 日
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

回答 (2 件)

Walter Roberson
Walter Roberson 2022 年 7 月 28 日
file2.m can call a function that is defined in file1.m under the following circumstances:
  • 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
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.
  1 件のコメント
Emily
Emily 2022 年 7 月 29 日
Hi, I'm a little lost, but it looks like I cannot call a nested function directly from outside. Thus have to create a classdef and create the nested functions there. I'm not quite sure how to accomplish this.
classdef
properties
a
b
c
end
methods
%%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

サインインしてコメントする。


Steven Lord
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.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by