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

7 ビュー (過去 30 日間)
MLP
MLP 2020 年 9 月 14 日
編集済み: Mario Malic 2020 年 9 月 18 日
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

採用された回答

Mario Malic
Mario Malic 2020 年 9 月 14 日
編集済み: Mario Malic 2020 年 9 月 15 日
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 件のコメント
MLP
MLP 2020 年 9 月 15 日
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 2020 年 9 月 15 日
編集済み: Mario Malic 2020 年 9 月 18 日
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';

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by