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

3 views (last 30 days)
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;

Accepted Answer

Jemima Pulipati
Jemima Pulipati on 17 Dec 2020
Edited: Jemima Pulipati on 17 Dec 2020
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 Comments
JUNGCHENG WU
JUNGCHENG WU on 25 Jan 2021
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 '????

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by