Same Name but Not Recognized Variable

12 visualizaciones (últimos 30 días)
Samantha
Samantha el 16 de Jul. de 2024
Editada: Stephen23 el 16 de Jul. de 2024
Hello everyone, I am having issues when calling upon variables. I have a bunch of variables in this code and when I try to call them or hover over them it doesn't highlight the others as the same.
I have included some pictures of my issue as I'm not quite sure how to describe it. The first one below shows me attempting to call a function, and the variables in calling that function are shown. However, when I call the function, I get this error: (in red of course)
The problem I've noticed is when I highlight a variable within calling that function, in the photo below I've highlighted "CurrThrottleData", it isn't highlighted everywhere else. A few places, but not in the function definition below.
Same issue is shown below with "fileName"
My next thought was maybe the function name is having issues or it's because I have the code separated into sections, but the function name lights up just fine in a different section. It previously worked when the function was called from a separate matlab file but it is really important that the function definitions and the code runs in one parent file for it all.
Any advice and help is appreciate, I have tried the following:
-Control f and replace everything with the same name but different capitalization to make sure it'd change it everywhere, this had no effect.
-Changing the name entirely to something different and manuall copy and pasting the name
-Copying the code in segments and running it in a new file
-Closing and opening matlab incase it needed a brain break... i guess haha
  3 comentarios
Samantha
Samantha el 16 de Jul. de 2024
Editada: Samantha el 16 de Jul. de 2024
I am still struggling to fully understand functions, it makes more sense now why it worked when the function was ran from a different file. Thank you for the response, insight, and hostility.
While I am still working on the code and adding debugging in the file, the function input variables ARE the same as the ones used in the calling script. The function is also defined at the end of the script.
Stephen23
Stephen23 el 16 de Jul. de 2024
Editada: Stephen23 el 16 de Jul. de 2024
"While I am still working on the code and adding debugging in the file, the function input variables ARE the same as the ones used in the calling script."
The variables inside the local function are different variables to those in your script. This is explained here:
The entire point of highlighting a variable is to show its scope. The variables defined in your local function have a completely different scope to those of your script, regardless of their names:
If you want them to share the same scope then you could turn your local functions into nested functions (this will mean turning your script into a function):
Then you can get the syntax highlighting that you expect, across multiple functions sharing one scope.
But a much better approach is to understand that local functions should have their own independent workspaces (and hence variable scope) and that the variable names used inside a function need not be the same as the ones used to call that function.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by