Undefined variable error while the variable was indeed defined right before the use

I am sorry to ask this again, but the previous answer was not even helpful.
My question is basically the same as describe here (see link) , but the answer did not explain things well and did not provide realization. I did try to add function handler, not working at all.
Attached code and error below:
%%Calculations
%To and Tw are previsouly imported tables
To.v = To.L / To.T; % unit = m/s
Tw.v = Tw.L / To.T; % unit = m/s
eta_oil = 2.675E-10* exp(5975.4/(16+273))
eta_water = 0.0011076 %unit = Pa*s
rho_oil = 0.878E3 % kg/m^3
rho_water = 1E3 %kg/m^3
rho_n = 1.13E3
rho_b = 8.45E3
To.Re = calcRe(To,0)
function Re = calcRe(T,type)
% Re = Dp*Vp*rho_f/eta_f
if type==0 % oil
Re = T.D .* T.v * rho_oil/eta_oil
else
Re = T.D .* T.v * rho_water/eta_water
end
end
The error is Undefined function or variable 'rho_oil'.
Workspace has eta_oil shown. I have no idea why the "snapshot" does not take eta_oil. Oh by the way, I also tried to declare the variable as global. Cannot resolve with effort. Please help ;-;

댓글 수: 2

_I have no idea why the "snapshot" does not take eta_oil._
What does that mean?
Oh the term snapshot was mentioned in the answer for the previous question (see link).

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

 채택된 답변

Stephen23
Stephen23 2018년 11월 1일
편집: Stephen23 2018년 11월 1일
@Kunhuan Liu: the reason is very simple: every function has its own workspace with its own variables:
Variables do NOT magically jump from one workspace to another workspace: to get variables into the function workspace, you will need to do this explicitly, e.g. by defining them as input/output arguments. There are some exceptions to this, e.g. anonymous functions and nested functions. The answer you linked to refers to an anonymous function, which is unrelated to the code that you show in your question. But you could certainly use an anonymous function in your code:
To.L = 1;
To.T = 1;
To.D = 1;
Tw.L = 1;
To.v = To.L / To.T; % unit = m/s
Tw.v = Tw.L / To.T; % unit = m/s
eta.oil = 2.675E-10*exp(5975.4/(16+273));
eta.water = 0.0011076; %unit = Pa*s
rho.oil = 0.878E3; % kg/m^3
rho.water = 1E3; %kg/m^3
rho_n = 1.13E3;
rho_b = 8.45E3;
Define and call and anonymous function:
>> fun = @(T,type)T.D .* T.v * rho.(type)/eta.(type);
>> fun(To,'oil')
ans = 3440.7
Or you could define a normal function and explicitly pass the required variables:
function Re = calcRe(T,type,rho,eta)
% Re = Dp*Vp*rho_f/eta_f
Re = T.D .* T.v * rho.(type)/eta.(type);
end
and then simply call it with those inputs:
>> calcRe(To,'oil',rho,eta)
ans = 3440.7
Do NOT use global variables. The best practice is to use input/output arguments or nested functions (as my answer shows):

댓글 수: 1

I see. So the scope of a function in Matlab is such that it does not access any variables outside of its own area. That will explain everything. Thanks!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2018년 11월 1일

댓글:

2018년 11월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by