transfer variables between functions?

47 views (last 30 days)
Coolman
Coolman on 3 Mar 2021
Edited: Jan on 4 Mar 2021
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

Accepted Answer

Jan
Jan on 3 Mar 2021
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 Comments
Coolman
Coolman on 4 Mar 2021
sorry missed that data thanks and now it works
Jan
Jan on 4 Mar 2021
Edited: Jan on 4 Mar 2021
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Event Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!