transfer variables between functions?

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

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

I know its unideal but I think its the only way to do it when I got it to do 10 different things in one piece of code without forcing all the variables at the start since depending on what function you want you do not need all of them. thank you for the help.
Can you suggest what I did wrong. here is the error.
Not enough input arguments.,Error in coursework>frictionFA (line 31) eqntwo = 1/sqrt(fA) == -2*log((data.De)/(3.7)+(2.51)/(data.RE*sqrt(fA)));,Error while evaluating UIControl Callback.
function single_data (~,~,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 ');
data.De = e/D;
syms s
eqn = (p*D*Q)/(u*A) == s;
data.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 (~,~,data)
syms fA
eqntwo = 1/sqrt(fA) == -2*log((data.De)/(3.7)+(2.51)/(data.RE*sqrt(fA)));
AS = vpasolve(eqntwo,fA);
fprint(AS)
end
Did you read my answer?
The function single_data does not need any inputs. Or is this a callback also called from anywhere?
In my answer you find the method to define the callback with the additional argument:
FA = uicontrol(f3,'Style','togglebutton','callback', {@frictionFA, data}, ...
% ^ ^^^^^^^
This is the way to provide variables to the callbacks.
sorry missed that data thanks and now it works
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 Function Creation in Help Center and File Exchange

Asked:

on 3 Mar 2021

Edited:

Jan
on 4 Mar 2021

Community Treasure Hunt

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

Start Hunting!