How can I use the if statement with a down drop in app designer?

11 次查看(过去 30 天)
I am making an app in which I calculate the value of a length with the angle and angular velocity provided by the user, I placed a down drop for w with two options: rpm and rad / s. In order to carry out the calculations I need the unit to be in rad /s.
The error that occurs when running the app is that in the lines of the variables z3C, z4C, z1C says that it does not recognize the variable w2.
I would appreciate your help.
% Callbacks that handle component events
methods (Access = private)
% Value changed function: DropDownW2
function DropDownW2ValueChanged(app, event)
uniw2 = app.DropDownW2.Value;
if strcmp(uniw2,'rpm')
wa2=app.w2.Value;
w2 = (2*pi*wa2)/60;
else
w2 = app.w2.Value;
end
end
% Button pushed function: CalcularButton
function CalcularButtonPushed(app, event)
a2 = app.a2.Value;
w3 = app.w3.Value;
a3 = app.a3.Value;
w4 = app.w4.Value;
a4 = app.a4.Value;
%Calular en complejos
z2C = w4*((w3^2)*j+a3)-w3*((w4^2)*j+a4);
z3C = w2*(a4+(w4^2)*j)-w4*(a2+(w2^2)*j);
z4C = w3*((w2^2)*j+a2)-w2*((w3^2)*j+a3);
z1C = -z2C-z3C-z4C;

采纳的回答

Jemima Pulipati
Jemima Pulipati 2020-12-17
编辑:Jemima Pulipati 2020-12-17
Hello,
From my understanding, you are not able to access the variables defined in one callback in another callback within the same app.
You have to use 'Properties' to access variables across functions and callbacks. You can either create a Public or Private Property, the process is explained here but here are the steps described briefly:
  1. From the editor tab in App Designer, select the red option 'Property' dropdown button at the top and select "Private Property". This will add a property definition to a properties block in the code.
  2. Within that newly added section, you can define any variable name that you want to use across callbacks, in your case 'w2' (see section 1 below)
  3. In your 'DropDownW2ValueChanged()' function, use 'app.' to reference this variable (declared in property) and assign some value. (see section 2)
  4. Now when you want to access this variable in any other callback use the same 'app.' to reference it. (see section 3)
% Section 1
properties (Access = private)
w2 = 0; % This variable is expected to be used across different callbacks
end
% Section 2
function DropDownW2ValueChanged(app, event)
app.w2 = (2*pi*wa2)/60; % Assigning values to the property variable declared for this app
end
% Section 3
function CalcularButtonPushed(app, event)
z3C = app.w2*(a4+(w4^2)*j)-w4*(a2+(app.w2^2)*j); % Using the same property variable in calculations
end
So, this means any property variable has to be referenced using 'app.' .This ensures the variable is present in the scope which is common to all callbacks / functions in the app.
NOTE: This property variable name should be unique. It should not be the same as any another property variables defined in another app and being passed to this app.
  2 个评论
Ana Lopez
Ana Lopez 2020-12-17
I was traying put to put the variable in the properties but it did not set the variable to zero, so I got this error 'Error using matlab.ui.control.internal.model.Abstract Numeric Component / set.Value (line 111)' Value 'must be a double scalar 'later. But this solved the problem and the app works as I want. Thanks you so much
JUNGCHENG WU
JUNGCHENG WU 2021-1-25
how did you solve the problem with ''Error using matlab.ui.control.internal.model.Abstract Numeric Component / set.Value (line 111)' Value 'must be a double scalar '????

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by