Loading a file into a function error: "Unable to resolve the name __"

I am trying to create a function that uses data from a file named Atmos.mat, however, no matter how I load the file, the function returns the error: "Unable to resolve the name Atmos.mat" Any help would be greatly appreciated :)
function x = rho(point)
load('Atmos.mat')
distance = zeros(1,229);
for k = 1:1:229
distance(k) = abs(point - Atmos.Hm(k));
end
for k = 1:1:229
if (distance(k) == min(distance))
break
end
end
x0 = Atmos.Hm(k);
p = @(x) (x - x0)/(Atmos.Hm(2) - Atmos.Hm(1));
f = @(x) p(x)*(p(x)-1)/2;
interpolation = @(x) Atmos.rhosi(k) + p(x)*(Atmos.rhosi(k + 1) - Atmos.rhosi(k))- f(x)*(Atmos.rhosi(k + 2) -2*Atmos.rhosi(k + 1)+ Atmos.rhosi(k));
x = interpolation(point)
end

2 Comments

does the error occur in the load() line or in the line you use the variable Atmos first? We don't know if your file Atmos.mat contains the variable Atmos
The error occurs in my first use of the variable Atmos.Hm (column entitled Hm within the Atmos file)

Sign in to comment.

 Accepted Answer

You have given two different errors.
"Unable to resolve the name Atmos.mat"
Check that Atmos.mat is either in your current folder when you call your function, or in a folder that is on your MATLAB path.
"The error occurs in my first use of the variable Atmos.Hm"
Check that your mat file contains a table or structure Atmos with a variable or fieldname Hm.

9 Comments

See this page for precedence.
For the 2nd error, in MATLAB, do the following
clear
load Atmos.mat
Check your workspace for a variable Atmos. Double click on it to open it in the Variable Editor to inspect its contents.
I did that and now receive the error: "Reference to a cleared variable point."
I'm afraid that is not enough context for me to interpret. When sharing an error message, share all the red text. Better still, attach your mat file to your post so we can run and test your code. You can use the paperclip icon to attach files.
>> rho
Reference to a cleared variable point.
Error in rho (line 7)
distance(k) = abs(point - Atmos.Hm(k));
Attached I added the rho.m function and the Atmos file
And how do you call your function (what is your input point)?
the input point is just a number. this function is simply called to find the linear interpolation about a certain point. so I would call it as rho(1000) to find the aproximated value of rho at 100 meters, according to the Atmos data set
Ok, thanks. Keep in mind that rho is a function that expects an input. You should always call it with an input.
In the function you have attached, you have added a clear to the top of your function. This is clearning your input variable. I see my early suggestion confused you. I wanted you to run that code in your Command Window, not add it to your function. Because you named your input variable points inside the function, and because you have a clear at the top of the function, you are getting this error.
>> rho(1000)
Reference to a cleared variable point.
Error in rho (line 7)
distance(k) = abs(point - Atmos.Hm(k));
Remove the clear from line 2.
When I inspect your mat file, I see it contains a table named Atmos1 instead of Atmos. Update your function to use the correct variable name, and as long as your function can access the mat file, your function should work.
Thank you thank you thank you!
You are an absolute life saver. Thank you for taking the time to walk me through this problem!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!