Why do I get an error when in my CallBack?

52 次查看(过去 30 天)
In my function my two inputs are numeric. I am trying to add input1 to input2 and have it appear in the Output box. I am able to edit my input boxes and insert values but my issue comes when i push my Add button. The third line of my code is where i get my error.
All the codes Ive tried:
% Button pushed function: AddButton
function AddButtonPushed(app, event)
x=app.Input1.Value;
y=app.Input2.Value;
app.Output.Value= addition(x,y);
end
Error message
Unrecognized function or variable 'addition'.
Error in EngrMaeFinal/AddButtonPushed (line 26)
app.OutputEditField.Value = addition(x,y);
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, ...
Error while evaluating Button PrivateButtonPushedFcn.
% Button pushed function: AddButton
function AddButtonPushed(app, event)
x=str2double(app.Input1.Value);
y=str2double(app.Input2.Value);
app.Output.Value = string(x+y);
end
Error while evaluating Button PrivateButtonPushedFcn.
Error using EngrMaeFinal/AddButtonPushed
'Value' must be a double scalar.
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, ...
Error while evaluating Button PrivateButtonPushedFcn.
  3 个评论
Abraham
Abraham 2023-9-10
app.Output.Value is the name of the output box that i want my sum to appear in
Walter Roberson
Walter Roberson 2023-9-10
app.Output is the name of the output box that you want your sum to appear in, and Value is a property of that box. I am asking what the class() of app.Output is, because the class() affects the permitted property values.

请先登录,再进行评论。

回答(1 个)

Sachin
Sachin 2023-9-13
Hi Abraham,
I understand that you are getting an error in the callback function. The issue could be because of an incorrect class type. For example: You are adding two double values and in the Edit field you are returning the result as a string. It will give an error if the Edit field accepts a double value. So, you must match the output with the accepted class type for that field.
For the 1st error, the issue is that there is no function named ‘addition’ in your code.
For the 2nd error, the issue could be because the field wants a double value to show but you are giving a string value.
Refer to the following steps to work with an example:
  1. Create a design like below by adding suitable fields.
2. Add a callback function for Button.
3. Implement the logic in the callback function.
I hope the above example helped in resolving the issue that you are encountering.
Thanks
Sachin

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by