필터 지우기
필터 지우기

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

조회 수: 40 (최근 30일)
Andrew
Andrew 2023년 11월 10일
댓글: Voss 2023년 11월 10일
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
  댓글 수: 2
Andrew
Andrew 2023년 11월 10일
이동: Voss 2023년 11월 10일
Thank you, Voss--this works well and I learned something! Very much appreciated.
Andy
Voss
Voss 2023년 11월 10일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (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