How do I pass data between functions in MATLAB App Designer?

40 次查看(过去 30 天)
I cannot get the syntax correct for passing data between functions using App Designer.
I have tried to follow the instructions in the article "Share Data within App Designer Apps" and can read in and plot data with the LoadHctdataButtonPushed function below.
However, when I invoke the function "BeginoptimizationButtonPushed" below, the value of the data (t, for example) is empty.
I can't see what I am doing differently from the example in the article. Any advice will be greatly appreciated!
Thanks,
Andy
-------------------------------------------------------
properties (Access = public)
t % time vector data
hct % hematocrit data
juf % ultra filtration data
% initial conditions
Vp0
Cp0
Vi0
Ci0
end
% Callbacks that handle component events
methods (Access = private)
% Callback function: LoadHctdataButton, UIAxes
function LoadHctdataButtonPushed(app, event)
filename=uigetfile;%; asks for data file
load(filename);% loads in t, hct(t) and juf(t) and initial SV values
yyaxis(app.UIAxes,'left')
plot(app.UIAxes,t,hct)
yyaxis(app.UIAxes,'right')
plot(app.UIAxes,t,juf,"Color",'r','LineWidth',2);
ylabel(app.UIAxes,'Juf (ml/min)')
app.Vp0EditField.Value=Vp0;
app.Cp0EditField.Value=Cp0;
app.Vi0EditField.Value=Vi0;
app.Ci0EditField.Value=Ci0;
app.NpointsEditField.Value=length(t);
end
% Button pushed function: BeginoptimizationButton
function BeginoptimizationButtonPushed(app, event)
t=app.t;
hct=app.hct;
juf=app.juf;
Vp0=app.Vp0;
Cp0=app.Cp0;
Vi0=app.Vi0;
Ci0=app.Ci0;

采纳的回答

Voss
Voss 2023-11-10
编辑:Voss 2023-11-10
load(filename) creates variables in a workspace (i.e., a function's workspace or the base workspace - in this case the workspace is the LoadHctdataButtonPushed function's workspace).
load(filename) does not set any app properties (even if the app has properties with the same name as the variables loaded).
S = load(filename) stores the variables in a struct S. You can then loop over the fields of S (i.e., the loaded variables), and set the corresponding app property (if it exists) to the same value.
Something like this:
function LoadHctdataButtonPushed(app, event)
[filename,filepath] = uigetfile(); %; asks for data file
if isequal(filename,0) % return if user cancelled
return
end
% load to struct S, and loop over fields of S, setting app properties
% when they exist to the values in S.
S = load(fullfile(filepath,filename));
fn = fieldnames(S);
for ii = 1:numel(fn)
if isprop(app,fn{ii})
app.(fn{ii}) = S.(fn{ii});
end
end
% then use the app properties here:
yyaxis(app.UIAxes,'left')
plot(app.UIAxes,app.t,app.hct)
yyaxis(app.UIAxes,'right')
plot(app.UIAxes,app.t,app.juf,"Color",'r','LineWidth',2);
ylabel(app.UIAxes,'Juf (ml/min)')
app.Vp0EditField.Value=app.Vp0;
app.Cp0EditField.Value=app.Cp0;
app.Vi0EditField.Value=app.Vi0;
app.Ci0EditField.Value=app.Ci0;
app.NpointsEditField.Value=length(app.t);
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by