Not sure what's wrong with my code

Having trouble calling the function altidense,
Whenever i try calling the function with the command "altidense(1500) i get a "Undefined function 'altidens' for input arguments of type 'double'"
I've been tearing my hair out because the code in the function itself is fine i tried by just inputting altitude directly and it worked fine. Its just when i call it as a function that makes the code fail.
Thanks
AltVsDens = readmatrix('Rho_ISA.csv');
altidense(1500)
%function to find air density from Rho_ISA.csv
%takes altitude and extracts air density from neighbouring cell
function airp = altidense(Altitude)
cell = (Altitude/500)+1;
airp = AltVsDens(cell,2)
end
%function to calculate true airspeed
%where m = mass, p = air density, s = wing area, Cl = coefficient of lift
function V = truspeed(m,p,s,Cl)
V = sqrt((2*m*9.81)/(p*s*Cl))
end

3 件のコメント

Johan
Johan 2022 年 2 月 22 日
編集済み: Johan 2022 年 2 月 22 日
You can't call a variable that is not part of a function in a function.
I would suggest adding your AltVsDens variable as an input to your function.
AltVsDens = readmatrix('Rho_ISA.csv');
altidense(1500,AltVsDens)
%function to find air density from Rho_ISA.csv
%takes altitude and extracts air density from neighbouring cell
function airp = altidense(Altitude,data)
cell = (Altitude/500)+1;
airp = data(cell,2)
end
William Xupravati
William Xupravati 2022 年 2 月 22 日
Thanks,
Do you know where i can read up more on this? because i dont fully understand why and how this works.
Cris LaPierre
Cris LaPierre 2022 年 2 月 22 日
It boils down to variable scope. Consider the following links

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2022 年 2 月 22 日

0 投票

The providing of variables as inputs and outputs as well as sharing the data using nested cells is explained in the free online tutorial: https://www.mathworks.com/learn/tutorials/matlab-onramp.html

カテゴリ

ヘルプ センター および File ExchangeFunctions についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 2 月 22 日

コメント済み:

2022 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by