Same Name but Not Recognized Variable
이전 댓글 표시
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
"However, when I call the function, I get this error"
Local functions cannot be called from the command line. Thus the error.
"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."
They are different variables in different workspaces. The fact that you have given them the same name is irrelevant: what happens inside the function workspace is independent of what happens in the calling script workspace. Highlighting both of them at the same time would be highly misleading (it would imply that values/changes in one workspace automatically have an effect on the other, which is incorrect in the general case. Again, the fact that you happen to have given two separate variables in two different workspaces the same name is irrelevant).
"it is really important that the function definitions and the code runs in one parent file for it all."
It does. You seem to assume that the variable names used in the function must be the same as the the names used in the calling script, or that giving them the same names has some kind of special meaning. It does not, it is merely a coincidence. The variable names used in a function can be (and often are) completely different to those used in the code that calls a function.
Do you know the names of any variables used in the code for SQRT() ? (hint: no)
Does that stop you from calling SQRT() ? (hint: no)
"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.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!