Accessing value of a variable inside a function

I have a variable, " c " defined outside a function as shown (just an example):
c=5;
function sum = add(a, b)
sum = a+b+c
end
Calling the add function by giving inputs for a, b Matlab throws an error saying : Local function name must be different from the script name. I want the value of the variable "c" to be accessed within the function so that if I give input of (2,3) for (a,b) I should be getting an output of sum = 10. Everything works fine if the variable " c " is inside the function but doesnt work when its outside the function. I tried to globally define the variable "c" like:
global c
c=5;
function sum = add(a, b)
global c
sum = a+b+c
end
But still Mathlab throws an error. Can anybody help me with this?

1 Comment

I suspect that you should be parameterizing the function:
Avoid global variables, they cause more problems than they solve.

Sign in to comment.

 Accepted Answer

The global case should not throw an error. However you should not use sum or add as the name of your variables since they're already built-in functions. Is the function in the same m-file as the script? When I do this, all in the same m-file, it works fine:
global c
c=5;
s = myAddition(10, 20)
function theSum = myAddition(a, b)
global c
theSum = a+b+c
end
So, does that work for you? If not, what did you do differently?

2 Comments

Is the function in the same m-file as the script? - Sorry I didnt understand this part. Were you asking whether my code is saved with the same name as that of the function? If so, yes - the m-file name was add.m
Do not call it add.m. That could cause a problem with the other built-in add functions. So rename it and upload/attach it here. If it's essentially the same as mine, it should run fine.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 30 Dec 2020
Edited: Walter Roberson on 30 Dec 2020
When you define a script, and you define a function inside the script, then the name of the function cannot be the same as the name of the script.
Also, when you define a variable in a script, functions defined in the script do not have access to the variable. Shared variables are possible only with nested functions.
I suggest that you read

9 Comments

Thank you. I would also like to access the value of a variable stored in one .m file in another .m file.
So in my case the variable (with its value ) ff=2.5 is stored in frequency.m file and I need to pass its value of 2.5 into another variable freq which is inside a function in another .m file - time.m.
The frequency.m file is (for simplicity):
...............
..............
ff =2.5
.......
.......
The time.m file is:
function result=detrend_phase_using_fresnel_frequency(phase,res,cutoption)
a1 = 1.931851652578;
freq = ff;
end
I did freq = ff (as shown above) to pass the value of ff into freq.
When I do this I am getting outputs based on the previously stored freq value and not the newly inputted ff value. However, when I explicitly type in 2.5 instead of ff (in freq = ff) I am getting the correct output. I was wondering why this is happening and if there exists any way for the variable freq to always take the value of ff without me explicitly typing in the value of ff always?
Upload add.m, time.m, and frequency.m.
Please find attached time.m and frequency.m files. You might also need the other attached files alongside to run without error.
frequency.m is the file which you should be running to see the output (others contain functions that aid the smooth running of frequency.m file)
Also below is the screenshots from the code where you could find the variables ff and freq ( NOTE: ff is defined inside frequency.m file and freq is defined inside the function of time.m file)
There are a couple of things more to avoid errors before running frequency.m file:
1) You might have to change the path in readGPS_using_fresnel_frequency.m file (line 9) since it contains mine at the moment.
2) You also might need to download BSFK function and install it from: https://www.mathworks.com/matlabcentral/fileexchange/25872-free-knot-spline-approximation
My problem is this:
When I run frequency.m with ff (=pp.breaks(2)) as defined in frequency.m and with freq=ff as in time.m I ge the below error saying : " unrecognized function or variable ' ff '.
However, if I explicitly type in the value of ff found after running frequency.m (one of the outputs seen in the command window is ff) and equate it to freq in time.m then I am not getting any errors and I am getting the results correctly.
For instance, suppose I got a value of ff = 0.8880 (in the command window) after running frequency.m. I now manually equate this value of 0.8880 with the variable freq in time.m (instead of freq = ff) as shown below:
This gives me the correct output (the one of interest to me is shown below):
Now when I change it back to freq=ff in time.m and run frequency.m I still get the same output as above (this time without any error). It seems that somehow the variable freq remembers the previous value and outputs result based on that. However, when I delete the folder in my path and run frequency.m again with freq=ff in time.m I am getting back the error ( However, I dont get any error even after I delete the folder in my path and run frequency.m again but with freq=0.888 ).
I am not sure what is happening. I have like thousands of ff values and manually equating every ff values to freq and then deleting the folder in the path everytime seems not practical.
Is there any way to automatically equate the ff values from frequency.m to freq=ff in time.m so that for every different ff value, I get the corresponding outputs (without the need to delete the folder in the path and without the need to manually enter the ff value).
Thanks.
I infact tried globally defining the variable ff in frequency.m and calling this global variable ff inside the function in time.m (before equating it as freq=ff).
The problem still persists - I will have to delete the folder which is created in the path everytime I run frequency.m inorder for the variable freq to assume the new ff value. If not, the previous value of ff seems to be stored inside freq giving me the same output as before (and not the output corresponding to the new ff ). I am unable to use clear global or clear all in frequency.m file since using that throws me abrupt errors.
Did you put
global ff;
inside BOTH frequency.m AND time.m?
Yes I did that as shown:
This is in frequency.m file
and this is in time.m file
Evidently using global variables does not help.
Pass the data reliably as input/output arguments or via nested functions.

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!