Unrecognized function or variable 'gca'

20 views (last 30 days)
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?

Accepted Answer

Walter Roberson
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
Image Analyst on 13 Jan 2022
The same danger occurs often when people redefine path (another built-in variable).

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 11 Jan 2022
This script works fine for me
plot(1:10)
f()
In f().
ans =
Axes with properties: XLim: [1 10] YLim: [1 10] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
function f()
fprintf('In f().\n')
gca
end
Are you sure you actually have an axes control visible?
  3 Comments
Walter Roberson
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.

Sign in to comment.


Fred Stanke
Fred Stanke on 12 Jan 2022
Thanks to Walter for suggestion above. I will look into that. Thanks to Image Analyst for asking and offering, but no, I can not send the function. It is way too proprietary. I have multiple prior versions, also proprietary, that do not have the issue. I was hoping for something like Walter's gotcha, and will check that out.
  2 Comments
Fred Stanke
Fred Stanke on 13 Jan 2022
Bingo! Well, almost !-) I had invented the syntax:
gca.XTickLabel=[];
as a shortcut to do something like
ha(2).XTickLabel=[]; %where ha is an axes handle
So ha(2).XTickLabel hosed the function without leaving a variable in the work space. With this line commented out, gca has its normal functionality. Thank you for your persistent patience!!!

Sign in to comment.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!