Accessing value of a variable inside a function

347 Ansichten (letzte 30 Tage)
Mahith Madhana Kumar
Mahith Madhana Kumar am 30 Dez. 2020
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 Kommentar
Stephen23
Stephen23 am 30 Dez. 2020
I suspect that you should be parameterizing the function:
Avoid global variables, they cause more problems than they solve.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 30 Dez. 2020
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 Kommentare
Mahith Madhana Kumar
Mahith Madhana Kumar am 30 Dez. 2020
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
Image Analyst
Image Analyst am 30 Dez. 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 30 Dez. 2020
Bearbeitet: Walter Roberson am 30 Dez. 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 Kommentare
Stephen23
Stephen23 am 31 Dez. 2020
Bearbeitet: Stephen23 am 31 Dez. 2020
Evidently using global variables does not help.
Pass the data reliably as input/output arguments or via nested functions.
Mahith Madhana Kumar
Mahith Madhana Kumar am 31 Dez. 2020
ok. I can try that.
Thanks

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by