App Designer, how to use value of DropDown in external function file

8 views (last 30 days)
Hello everyone, currently I am working on App Designer and need to use the DropDown item in order to be able to select which expression I want the calculations to proceed with. I have created a separate .m file which involves an if statement. If the DropDown value (Type) equals 1, do this expression, if it equals 2, then this other. I am getting the following error "Output argument "Rad_fin" (and maybe others) not assigned during call to "OgiveTypeAPP"". I call the outputs on app designer as follows:
[Rad_fin,Rad_tip,Rad_rear,Rad] = OgiveTypeAPP(dist_fin,DIST_tip,DIST_rear,DIST); %All inputs are specified on the app.
On app desginer I have stored the value of the dropdown as:
global Type; Type = app.DropDown.Value;
Below is a screenshot of the settings for the drop down and the external .m function I am using.
function [Rad_fin,Rad_tip,Rad_rear,Rad] = OgiveTypeAPP(l,m,n,o)
global Rmax
global L
global Type
if Type == 1
Rad_fin = Rmax*sqrt(l/L);
Rad_tip = Rmax*sqrt(m/L);
Rad_rear = Rmax*sqrt(n/L);
Rad = Rmax*sqrt(o/L);
elseif Type == 2
Rad_fin = Rmax*(l/L).^(0.75);
Rad_tip = Rmax*(m/L).^(0.75);
Rad_rear = Rmax*(n/L).^(0.75);
Rad = Rmax*(o/L).^(0.75);
end
end

Accepted Answer

Mario Malic
Mario Malic on 14 Sep 2020
Edited: Mario Malic on 15 Sep 2020
Quick, delete the line with global variable before MATLAB police comes! Don't use global variables, unless there is really a need for it.
First of all, if you will use your variables in external functions, use the as properties, there is an option called Create property - public and define it.
Rmax = 5; % or Rmax = []
Similarly for char array: Type ='';
You can access or change the property value by typing
app.Rmax
In App designer create a button and a callback. Within it call your external function and pass the whole app (or only needed variables)
ExternalFunction(app)
And within the external function access the variables/properties as mentioned above.
  2 Comments
MLP
MLP on 15 Sep 2020
Thank you for your help, I got rid of the global variables!
I had to use the strcmp function and use the ItemData value, I don't really understand why but it works perfectly. Thank you again! Here is how I wrote it in case someone else stumbles upon this question.
function [Rad_fin,Rad_tip,Rad_rear,RAD] = OgiveTypeAPP(l,m,n,o,p,q,s)
Rmax = p; L = q;
if (strcmp(s,'1')) %Conical
Rad_fin = (Rmax*l)/L;
Rad_tip = (Rmax*m)/L;
Rad_rear = (Rmax*n)/L;
RAD = (Rmax*o)/L;
elseif (strcmp(s,'2')) %Elliptical
Rad_fin = Rmax*sqrt(1-((l-L).^2)/L^2);
Rad_tip = Rmax*sqrt(1-((m-L).^2)/L^2);
Rad_rear = Rmax*sqrt(1-((n-L).^2)/L^2);
RAD = Rmax*sqrt(1-((o-L).^2)/L^2);
Mario Malic
Mario Malic on 15 Sep 2020
Edited: Mario Malic on 18 Sep 2020
I am not sure why didn't you get an error in your App Designer since Value can only be equal to Items (maybe I am wrong). In your example
DropdownMenu.Items = {'Power Law n = 0.5; Power Law n = 0.75'};
DropdownMenu.Value = 'Power Law n = 0.5'; % Must be one of the Items
ItemsData is a number that is associated with Items, you set there already 1 and 2.
Therefore
DropdownMenu.ItemsData = 1 - corresponds to DropdownMenu.Items{1} = 'Power Law n = 0.5' and DropdownMenu.Value(1) = 'Power Law n = 0.5';

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!