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

조회 수: 5 (최근 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 CenterFile Exchange에서 Develop 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