How To Call Function In App Designer
Code and Call App Functions in App Designer
There are four types of app functions:
-
A function that runs when the app user starts the app.
This is the startup function for the app. Use a startup function, for instance, to initialize properties.
For an example, see App Designer Startup Function.
-
A function that runs when the app user manipulates a control.
This is a callback function for the component. Use a callback function, for instance, to display the value of a slider in a numeric edit field as the app user moves the slider.
For an example, see Code Response to Reflect Changing Slider Value.
-
A utility function that performs a task that you can reuse across your app, but not outside it.
This is a private utility function. Use a private function, for instance, if you need to use the same code in multiple callbacks. You can code a private function containing that code, and then call the private function from each callback.
For an example, see Create Private and Public Utility Functions.
-
A utility function that performs a task that you can use outside (and across) your app.
This is a public utility function. Use a public function, for instance, if you need to share the function with other apps, the MATLAB® workspace, or devices attached to your computer system.
For information on how to create a public function, see Create Private and Public Utility Functions.
Create Private and Public Utility Functions
Create a private or public utility function from App Designer code view, as follows:
-
On the Editor tab, click the Function down arrow, and then select Private Function or Public Function.
Unless you intend to use the function outside the current app, choose Private Function.
-
App Designer adds the framework for your function after the properties block in your code.
Except for the
Access
attribute, the framework for private and public functions is identical.The first time you add a private function, App Designer creates a private method block. If you add additional private functions, they are added to this method block. Similarly, App Designer creates a public method block the first time you add a public function to your app.
-
Replace the highlighted text,
func
, with a meaningful name for your function. -
Optionally, replace
results
with one or more output arguments and add additional input arguments. For example:methods (Access = private) function [a b] = PopulationEst(app,c) end end
If your function does not require output or return a value, you can delete the
results =
syntax from the function declaration. -
Add your function code.
Run your code and debug it, if needed, as described in Detect and Correct Coding Errors Using App Designer.
Create and Call a Private Utility Function
This example shows how to share a private utility function throughout an app. It uses a private function, mycalc
, to calculate the sum and product of two values specified by the app user. When the app user changes either value, the app recalculates the sum and product by calling the mycalc
function.
In App Designer:
-
Drag four numeric edit fields (with labels) from the Component Library on the left of App Designer into the design area. Arrange them vertically as shown in the preceding image. By default, when you drag a numeric edit field into the design area, an associated label is also added to the design area.
-
Change the edit field labels. Double-click each edit field label to make the following changes in the canvas:
-
Set the first (top) label to
Value1
. -
Set the second label to
Value2
. -
Set the third label to
Sum
. -
Set the fourth (bottom) label to
Product
.
-
-
Disable editing for the bottom two edit fields:
-
Select
app.SumEditField
in the Component Browser. Locate the Edit Field Properties panel and scroll down to the General section. Deselect (clear) the Editable check box. -
Select
app.ProductEditField
in the Component Browser, and deselect the Editable check box in the Edit Field Properties panel.
-
-
Click Code View.
-
Create a function to calculate the sum and the product of the values that the app user enters for value 1 and value 2:
-
On the Editor tab, click Function > Private Function.
Note: The Function button is disabled when there are syntax or parse errors in your utility functions or the properties block. For information on debugging, see Detect and Correct Coding Errors Using App Designer.
-
Replace the blue highlighted text,
func
, with a meaningful name for this function,mycalc
. -
Delete the output argument,
results
. -
Code the function to get the app-user-entered values, perform calculations, and assign values to the function output arguments,
sum
andproduct
.The function appears as follows:
function mycalc(app) a = app.Value1EditField.Value; b = app.Value2EditField.Value; sum_result = a + b; prod_result = a * b; app.SumEditField.Value = sum_result; app.ProductEditField.Value = prod_result; end
-
-
Code the callback for the
Value1
numeric edit field to call themycalc
function whenever the app user changes the field value:-
In the Component Browser, right-click app.Value1EditField and select Callbacks > Add ValueChangedFcn callback.
-
In the Add Callback Function dialog box, click OK.
-
Replace the default
Value1EditFieldValueChanged
function code with the following code. The code calculates the sum and product and update the corresponding numeric edit fields:
-
-
Code the callback for the
Value2
numeric edit field to call themycalc
function whenever the app user changes the field value:-
In the Component Browser, right-click app.Value2EditField and select Callbacks > Add ValueChangedFcn callback.
-
In the Add Callback Function dialog box, then click OK.
-
Replace the default
Value2EditFieldValueChanged
function code with the following code:
-
-
On the Toolstrip, click Run.
Save the app when prompted.
Test the app by entering numbers in the Value 1 and Value 2 fields and then clicking away from the fields.
Call a Public Utility Function
You can call a public utility function from a different app than the one in which it is defined. To do so, an instance of the app that defines the utility function must exist in the workspace of the app that calls the function.
For example, suppose that you create an app getInput
, that contains a public function, stats
. To call the stats
function from a second app, plotStats
, plotStats
must contain code that calls getInput
and assigns the output to a property.
The following plotStats
code assigns the output from getInput
to the property getdata
.
Then the plotStats
app can call the stats
function defined getInput
by using a command such as this:
results = app.getdata.stats
Related Examples
- App Designer Callbacks
- Delete Code from the App Designer Editor
- Detect and Correct Coding Errors Using App Designer
Was this topic helpful?
How To Call Function In App Designer
Source: https://lost-contact.mit.edu/afs/inf.ed.ac.uk/group/teaching/matlab-help/R2016b/matlab/creating_guis/code-and-call-app-functions-in-app-designer.html
Posted by: elliotalcon1969.blogspot.com
0 Response to "How To Call Function In App Designer"
Post a Comment