Unrecognized function or variable 'gca'
21 views (last 30 days)
Show older comments
Fred Stanke
on 11 Jan 2022
Commented: Image Analyst
on 13 Jan 2022
How can it happen that in a calling function "gca" works, but in the called function "gca" reports "Unrecognized function or variable 'gca'."
In the calling function, just before executing:
[Mlk_b_h,TierParms] = Fourier_SE_v15(NK_FM,To,ToolParms);
gca works.
if I dbstop on first line of Fourier_SE_v15:
K>> gca
Unrecognized function or variable 'gca'. Why did 'gca' disappear?
0 Comments
Accepted Answer
Walter Roberson
on 13 Jan 2022
Summarizing:
gca was being assigned to as a variable. With current versions of MATLAB, if you assign to a variable that has the same name as a function, then you can become unable to call the function, even if the assignment to the variable has not yet taken place.
1 Comment
Image Analyst
on 13 Jan 2022
The same danger occurs often when people redefine path (another built-in variable).
More Answers (2)
Image Analyst
on 11 Jan 2022
This script works fine for me
plot(1:10)
f()
function f()
fprintf('In f().\n')
gca
end
Are you sure you actually have an axes control visible?
3 Comments
Walter Roberson
on 12 Jan 2022
I know of one circumstance that can lead to that kind of problem:
If you are inside a function, and the function has a matching end statement for the function declaration, and the function assigns to a variable that has the same name as another function (for example, assigns to sum or gca), but you have not yet executed the assignment (for example you might be stopped at a breakpoint), then in that case, you might not be able to use the name as a function.
For example,
function test
x = rand()
%location 1
if x < 0.5
gca = 7
end
%location 2
disp(x)
end
At location 2, asking
gca
could be either the number 7 or the call to the function, depending on the flow of control. In the last several releases, MATLAB has said that you cannot use the same name as a function and a variable, so at location 2, gca would not be permitted to refer to the function, but at the same time gca might not have been assigned to, so you might not be able to display gca as a variable. MATLAB can instead say "No such function or variable" for gca there.
Furthermore, the block assigns to the whole function: if you were to ask to gca at location 1 before the (possible) assignment to gca, then you cannot be referring to the function because MATLAB no longer allows the same name to be a function and a variable inside a function -- so you could get the same "No such" etc. at location 1 as well.
Fred Stanke
on 12 Jan 2022
2 Comments
Image Analyst
on 13 Jan 2022
Simply search for "gca = " or "gca=" or "gca(" to see if you assigned anything to gca or declared gca as a function. Should be real quick to learn that.
See Also
Categories
Find more on Specifying Target for Graphics Output in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!