transfer variables between functions?

조회 수: 52 (최근 30일)
Coolman
Coolman 2021년 3월 3일
편집: Jan 2021년 3월 4일
I am trying to use a function to develop my variables for the rest of my code since its used multiple times. here is an example. try to keep it simple since I just recently started learning MATLAB. basically want De and RE to be used in the function below.
function single_data (~,~)
p = input('density/kg/m^3 ');
e = input('surface roughness/m ');
D = input('diameter/m ');
Q = input('volumetric flowrate/m^3/s ');
u = input('viscosity/pa/s ');
A = input('cross-sectional aream/m^2 ');
De = e/D;
syms s
eqn = (p*D*Q)/(u*A) == s;
RE = vpasolve(eqn,s);
f3 = figure(3);
FA = uicontrol(f3,'Style','togglebutton','callback', @frictionFA,'String','automated(vpasolve) ','Position',[20 20 200 30]);
FBS = uicontrol(f3,'Style','togglebutton','callback', @frictionFBS,'String','bisection method','Position',[20 50 200 30]);
FNR = uicontrol(f3,'Style','togglebutton','callback', @frictionFNR,'String','Newton Rhapson Method ','Position',[20 80 200 30]);
end
function frictionFA (~,~)
syms fA
eqntwo = 1/sqrt(fA) == -2*log((De)/(3.7)+(2.51)/(RE*sqrt(fA)));
AS = vpasolve(eqntwo,fA);
fprint(AS)
end

채택된 답변

Jan
Jan 2021년 3월 3일
Variables cannot be "transfered" between functions. You need to provide them as inputs and outputs.
Combining GUI and the actual code makes it much harder to maintain the code. You have to struggle with callbacks and sharing data at the same time, so this is a bad design. But it works:
Store the data to be shared in a struct and provide them as inputs to the callback:
data.eqn = (p*D*Q)/(u*A) == s;
data.RE = vpasolve(eqn,s);
FA = uicontrol(f3,'Style','togglebutton','callback', {@frictionFA, data}, ...
function frictionFA (~,~, data)
disp(data.RE) % Just as demonstration
...
end
  댓글 수: 5
Coolman
Coolman 2021년 3월 4일
sorry missed that data thanks and now it works
Jan
Jan 2021년 3월 4일
편집: Jan 2021년 3월 4일
As said already, it would be easier to separate the parts for the computing and the GUI. E.g.:
% The code for processing:
function Core(Command)
persistent data
if isempty(data) && ~strcmp(Command, 'clear')
p = input('density/kg/m^3 ');
e = input('surface roughness/m ');
D = input('diameter/m ');
Q = input('volumetric flowrate/m^3/s ');
u = input('viscosity/pa/s ');
A = input('cross-sectional aream/m^2 ');
data.De = e/D;
syms s
eqn = (p*D*Q)/(u*A) == s;
data.RE = vpasolve(eqn,s);
end
switch Command
case 'FA'
syms fA
eqntwo = 1/sqrt(fA) == -2*log(data.De/3.7+2.51/(data.RE*sqrt(fA)));
AS = vpasolve(eqntwo,fA);
fprint(AS)
case 'FBS'
...
case 'FNR'
...
case 'clear' % remove the persistent data
data = [];
otherwise
error('Unknown command: %s', Command);
end
end
% Another M-file with the GUI: -------------------------------
function myGUI
% Maybe reset the persistent data in Core:
Core('clear');
f3 = figure(3);
FA = uicontrol(f3, 'Style','togglebutton', ...
'callback', {@myCallback, 'FA'},'String','automated(vpasolve) ', ...
'Position',[20 20 200 30]);
FBS = uicontrol(f3, 'Style','togglebutton', ...
'callback', {@myCallback, 'FBS'},'String','bisection method', ...
'Position',[20 50 200 30]);
FNR = uicontrol(f3, 'Style','togglebutton', ...
'callback', {@myCallback, 'FNR'},'String','Newton Rhapson Method ', ...
'Position',[20 80 200 30]);
end
function myCallback(~, ~, Command)
Core(Command);
end
end
Now you can call your code by a another function or through the GUI. Both parts can be maintained separately from each other.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Event Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by