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

9 次查看(过去 30 天)
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!

采纳的回答

Bhanu Prakash
Bhanu Prakash 2023-10-9
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:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by