Passing input arguments into a function in Matlab AppDesigner

68 Ansichten (letzte 30 Tage)
mobius
mobius am 1 Apr. 2020
Kommentiert: mobius am 3 Apr. 2020
Hey guys,
I'm new to Matlab AppDesigner (using 2018b). I have a external m-file function which I want to call in Matlab AppDesigner, pass input into it and obtain the output. I declared the variables in the Property section and functions in the Function section.
Below is the code snippet for my Function section, where I define the functions:
methods (Access = public)
function [meanErr,maxErr,timerVal,hmid2,hamid2] = validate(app)
[app.meanErr,app.maxErr,app.timerVal,app.hmid2,app.hamid2] = ...
validateD2Q9(app.N_iter,app.len,app.wid,app.lx,app.ly,app.Cs,app.rho,app.tau,app.depth,app.A);
end
end
Then, I called the function validate(app) in pushbuttoncallback:
function RunButtonPushed(app, event)
[app.meanErr,app.maxErr,app.timerVal,app.hmid2,app.hamid2] = validate(app);
plot(app.UIAxes,app.hmid2,'LineStyle',':','Color','b');
plot(app.UIAxes,app.hamid2,'LineStyle','-','Color','r');
end
When I run the GUI, I get the following error:
Error using linspace (line 22)
Inputs must be scalars.
Error in validateD2Q9 (line 14)
X = linspace(0,len,lx); Y = linspace(0,wid,lx); %coordinate of each lattice
Error in airylbm2d_app/RunButtonPushed (line 109)
[app.meanErr,app.maxErr,app.timerVal,app.hmid2,app.hamid2] = validate(app);
Previously if I run the m-file directly, everything works fine. Why am I getting this error? Is it because of some syntax mistakes I've made?
Cheers and thanks
  1 Kommentar
mobius
mobius am 3 Apr. 2020
Hey guys,
I found where the problem is. In the field components on the GUI, default parameters are specified. However, the callback for each field components only executes if field values are changed. The variables can be added by clicking the green '+' button in the Properties tab (upper left). The code block for Properties are as follows:
properties (Access = public)
% Define all input and output variables
A; output
end
In my previous code, I've declared all properties as per above, and defined callbacks as well. Below is one of the many callback functions for field components, where I assigned the field values into the variable 'A'. Notice that in the comment, it says 'value changed function', i.e. this function is only executed if field value changes.
% Value changed function: IterationEditField
function IterationEditFieldValueChanged(app, event)
app.A = app.AEditField.Value;
end
However, I intended to have a default value in the field components until the user changes them, which means the code does not have instructions to extract default values (since the values are only extracted if the user changes them). Thus what I did was to include a 'startupFcn' code block, which executes when the whole GUI starts. The 'startupFcn' code block can be added by clicking '+' in the Callbacks tab (upper left), choose UIFigure in 'Components' and then startupFcn in 'Callback'.
% Code that executes after component creation
function startupFcn(app)
%Extract values from field components during startup
%if field values changed, callbacks in the following section will execute to extract values
app.A = app.AEditField.Value;
end
Lastly, in the 'pushbuttoncallback' function, I called the function from a m-file like usual.
% Button pushed function: RunButton
function RunButtonPushed(app, event)
%when push button is clicked
%declare global here for all input and outputs
global A
A = app.A;
%Run intended function from some m-file
[output] = YourFunctionName(A);
app.output = output;
end
Then you can assign the output into other a property just like in the code above.
Long story short, the program needs to extract values upon startup, since the callbacks for edit-field components are only for condition where the value is changed by the user.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by