How to call the function A in another function B under the static methods of the same APP Designer?

Hello! I'm learning how to design a MATLAB app with MATLAB APP Designer. I' ve written some static functions as the following examples, but I couldn't call the function "add1" in the funtion "test".
methods (Static)
function test(a, b)
c = add1(a, b); %Error: Undefined function 'add1' for input arguments of type 'double'.
fprintf('The results is %d', c);
end
function z = add1(x, y)
z = x + y;
end
end
I' m not familiar to the OOP. Could you guide me what mistakes I made and how to call the "add1" correctly in addition to nest it in "test"?
Thank you!

 Accepted Answer

Hi Qiang,
I understand that you want to call the function add1 from a different function test, which is under the static methods of the same App Designer class.
One simpler way to accompish this is to define a class using the 'classdef' function in MATLAB, inheriting the base class 'matlab.apps.AppBase' to utilize the built-in functionality provided by App Designer.
Consider the following code:
classdef myApp < matlab.apps.AppBase
methods (Static)
function test(a, b)
c = myApp.add1(a, b);
fprintf('The result is %d\n', c);
end
function z = add1(x, y)
z = x + y;
end
end
end
where, 'myApp' is the name of the App Designer class. You can call the function 'add1' from the 'test' function as follows:
>>myApp.test(2,3)
The result is 5
For more information on the 'classdef' function, kindly refer to the following documentation:

1 Comment

Dear friend,
Thank you for your help! I have practiced it in my app project and it works well.

Sign in to comment.

More Answers (0)

Categories

Find more on Software Development Tools in Help Center and File Exchange

Products

Asked:

on 9 Oct 2023

Commented:

on 10 Oct 2023

Community Treasure Hunt

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

Start Hunting!